8

I got into a lot of trouble when changing the permission of a folder myfolder residing in /. I issued the command

sudo chown -R luca:luca /myfolder/.*

My intention was to change ownership of all the hidden files in /myfolder.

Unfortunately I realized that also the ownership of / was changed, which of course left me with a broken system. I think this happened because .. matches .*, but still seems weird to me.

Is changing the parent directory the correct behaviour or should I file a bug report?

If it was my mistake in using chown, what are the best practices to use to prevent changing the ownership of system folders and files?

  • 4
    This is intended behaviour as .. matches .* – Uwe Plonus Jul 5 '13 at 12:18
  • .* means any file that ends with . *. means any file that starts with . – NickTux Jul 5 '13 at 12:22
  • 4
    @NikTh It the other way round! – Uwe Plonus Jul 5 '13 at 12:26
  • Well one best practice might be not modifying / directly (creating folders, removing, etc) unless utterly necessary (open a question for what you wanted to achieve). Also is dangerous using wildcards+root privileges because, more often than not, you are not 100% sure what is been affected by the wildcard. – edwin Jul 5 '13 at 12:35
  • 1
    @LucaCerone It is related, chmod and chown are similar commands, both start with ch, contains an o and have two other similar letters (n and m). </literal> Seriously, they are both of the form: [cmd] -R [mode or user] [one or more files]. Some solutions offered on that question work for your case too, for instance this one from Sean Reifschneider (in the middle). This answer is also applicable to hidden files only by removing the glob pattern for non-hidden files. – Lekensteyn Jul 5 '13 at 14:18
5

Remember that the command line is expanded (interpreted) by the shell before being executed

sudo chown -R luca:luca /myfolder/.*

is interpreted first as :

sudo chown -R luca:luca /myfolder/.  /myfolder/.. /myfolder/.adobe  /myfolder/.bash_history

note the /myfolder/.. in your command line

chown -R luca:luca /myfolder/.. is equivalent to chown -R luca:luca /
that makes the chown running "backwards"

Use echo /myfolder/.* when you use "*" to verify .

  • @Emmanuel: why would you use xargs and find when the -R option is sufficient? – January Jul 5 '13 at 12:46
  • @Emmanuel: thanks! can you expand a bit about the find and xargs? – lucacerone Jul 5 '13 at 14:10
  • @LucaCerone I am not sure why you accepted this one, it recursively changes all files in /myfolder. The find command is superfluous and breaks with filenames containing whitespace. (if you want to recursively change files, find /myfolder | xargs chown luca:luca is similar to chown -R luca:luca /myfolder). – Lekensteyn Jul 5 '13 at 14:39
  • Lekensteyn is right I forgot to grep ^. – Emmanuel Jul 5 '13 at 15:09
  • @Lekensteyn I changed the find to not match space – Emmanuel Jul 5 '13 at 15:23
4

Well. Command line as root is very powerful. Read some of these classics. And yes, .* matching to .. is exactly what is intended. Dot is not a special character. It is a convention. By convention, files that start with a dot are hidden from the default view when listing a directory -- nothing less, and nothing more. By convention, the inode leading to the current directory gets the . name and the inode leading to the parent directory gets the .. name.

What you should have done was

chown -R luca:luca /myfolder

Did I mention that there is nothing special about the file names that start with a dot? Recursive chown doesn't think so.

Right now, you might be able to rescue some of the functionality by changing the ownership back to root. In the long run, you will probably have to reinstall the system, though.

As a general rule:

  1. Avoid working as root.
  2. If you work as root, read each command twice before hitting Enter.
  3. If you are unsure about expansion, try it first with a "safe" command (like echo .*).
  4. Do not work as root.
  5. There are many tasks that can be performed safely using a graphical interface (your problem is an example of such a task).
  6. Did I mention that you should avoid using the root account?
  • I didn't want to change the ownership of all the files in the folder... only of the hidden files.. – lucacerone Jul 5 '13 at 14:09
  • 2
    OK, my bad then. In that case, chown -R luca:luca /myfolder/.[^.]* I think – January Jul 5 '13 at 14:19
  • 2
    echo .* is a good way of trying out the expansion. It'll show you exactly what the shell sees. ls .* can be a bit more confusing, as it'll go down directories (ls -d .* might be better, but might as well just use echo .*) – Robie Basak Jul 5 '13 at 15:15
  • @RobieBasak good point! – January Jul 5 '13 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.