Ubuntu – Recursive chown starting with the directory above current directory

chowncommand line

I couldn't log in to my "admin" account and Alt+Ctrl+F1 showed all my files were owned by my "standard" user. Odd.

So I carefully changed to /home/admin and did a

sudo chown -R admin:admin *

(and .* too).

Great.

Then I couldn't log in as my "standard" user and it turns out that all the files in /home/standard were now owned by "admin".

Pretty humorous. Why is this happening?

sudo chown -R standard:standard /home/standard/*

did the same thing, changed /home/admin as well as /home/standard.

I'm more confused than usual because I tried to upgrade to 15.04 and that pretty much wrecked my computer so I'm putting things back with 14.04, please be patient with me.

Best Answer

This issue is caused because you have run:

sudo chown -R admin:admin .*

We know that . indicates the current directory and .. indicates the parent directory. When you run the command with .*, it simply means that match any hidden file in the current directory (stating with .), the current directory itself (.), the parent directory (..). Simply put anything after . (* means 0 or more characters). As a result the parent directory along with all of it child directories get chown-ed to admin:admin.

Look at this test:

test$ ls -al
drwxrwxr-x 4 foo foo 4096 Jun  3 07:15 .
drwxrwxr-x 4 foo foo 4096 Jun  2 18:06 ..
drwxrwxr-x 2 foo foo 4096 Jun  3 07:15 egg
drwxrwxr-x 2 foo foo 4096 Jun  3 07:12 spam

$ sudo chown -R bar:bar spam/.*

test$ ls -al
drwxrwxr-x 4 bar  bar  4096 Jun  3 07:15 .
drwxrwxr-x 4 foo  foo  4096 Jun  2 18:06 ..
drwxrwxr-x 2 bar  bar  4096 Jun  3 07:15 egg
drwxrwxr-x 2 bar  bar  4096 Jun  3 07:12 spam

To revert back you need to chown the affected directories again.

I am not really sure what your plan was, but here are some ideas:

  • To chown any directory recursively (including hidden files):

    sudo chown -R foo:foo /spam/egg/
    
  • To chown only the files (including hidden files) inside that directory (not the directory itself):

    (shopt -s dotglob && sudo chown -R foo:foo egg/*)
    
  • To chown only the non-hidden files (without the directory itself):

    sudo chown -R foo:foo egg/*
    
Related Question