chown -R user:user .* changes permissions backwards: is this the correct behaviour?

52,827

Solution 1

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 .

Solution 2

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?
Share:
52,827
lucacerone
Author by

lucacerone

Updated on September 18, 2022

Comments

  • lucacerone
    lucacerone over 1 year

    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?

    • NickTux
      NickTux almost 11 years
      .* means any file that ends with . *. means any file that starts with .
    • Uwe Plonus
      Uwe Plonus almost 11 years
      @NikTh It the other way round!
    • edwin
      edwin almost 11 years
      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.
    • Lekensteyn
      Lekensteyn almost 11 years
    • Lekensteyn
      Lekensteyn almost 11 years
      @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.
    • lucacerone
      lucacerone almost 11 years
      @Lekensteyn thanks for the links, (I didn't know about shopt..) I still think though that the purpose of AskUbuntu is provide help to newbies the that can not know that chown and chmod have the same syntax :) (I discovered it today :))
    • Lekensteyn
      Lekensteyn almost 11 years
      @LucaCerone That link is from Serverfault.com by the way; I find myself sometimes running chmod 0:0 [file] instead of chown 0:0 due to the similarity. Are you wondering what 0:0 means? RTFM! :D
  • January
    January almost 11 years
    @Emmanuel: why would you use xargs and find when the -R option is sufficient?
  • lucacerone
    lucacerone almost 11 years
    I didn't want to change the ownership of all the files in the folder... only of the hidden files..
  • lucacerone
    lucacerone almost 11 years
    @Emmanuel: thanks! can you expand a bit about the find and xargs?
  • January
    January almost 11 years
    OK, my bad then. In that case, chown -R luca:luca /myfolder/.[^.]* I think
  • Lekensteyn
    Lekensteyn almost 11 years
    @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).
  • Emmanuel
    Emmanuel almost 11 years
    Lekensteyn is right I forgot to grep ^.
  • Anish
    Anish almost 11 years
    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 .*)
  • Emmanuel
    Emmanuel almost 11 years
    @Lekensteyn I changed the find to not match space
  • Emmanuel
    Emmanuel almost 11 years
    @January the find command alone lets you check first if you are modifying the files you want, then you can do the find piped into xargs
  • lucacerone
    lucacerone almost 11 years
    @Lekensteyn I voted this because clearly explains how the filenames are expanded... and helped me understand where things went wrong.