How to Chown a directory recursively including hidden files or directories

106,269

Solution 1

I'm pretty sure the -R flag does work - it always has for me anyway. What won't work, and what tripped me up early in my command line usage, is using * in a directory with hidden files/directories. So doing

$ chown -R /home/user/*

will not do the hidden files and directories. However if you follow it with

$ chown -R /home/user/.[^.]*

then you will do all the hidden files, (but not . or .. as /home/user/.* would do). Having said all that, I would expect

$ chown -R /home/user

to get all the hidden files and directories inside /home/user - though that will of course also change the permissions of the directory itself, which might not be what you intended.

Solution 2

i believe the following command should work for this

chown -hR userid:usergroup /nameofdirectory/nameofsubdir/

Solution 3

"chown -R" works, but an alternative would be using find.

 find /path/to/dir -exec chown USER {} \;

Solution 4

Also, if you're like me you'll probably be running chown mostly from the current directory. I was accustomed to running it like this: chown rails.rails -R * . Simply changing the asterisk to a dot (short for the current directory) like this: chown rails.rails -R . brings in all hidden directories.

Solution 5

You can change the dotglob attribute temporarily to expand . files and then revert it.

shopt -s dotglob; chown -R user:group FOLDER; shopt -u dotglob

More on dotglob can be found here

Share:
106,269

Related videos on Youtube

user4723402
Author by

user4723402

Some favorite languages: Javascript, Python, Ruby, Haskell, ActionScript.

Updated on September 17, 2022

Comments

  • user4723402
    user4723402 almost 2 years

    Seems like chown with the recursive flag will not work on hidden directories or files. Is there any simple workaround for that?

  • wfaulk
    wfaulk about 12 years
    Doing a chown on the directory has the side effect that you change the permissions on the directory itself as well as all of its contents, which may or may not be what you want.
  • wfaulk
    wfaulk about 12 years
    This will change only files and subdirectories in the current directory, not any lower levels. (Which may be what the OP wants.) It will also break on filenames and directory names with spaces (or tabs) in them.
  • stew
    stew about 12 years
    note that with GNU find, using + instead of ; as the terminator to the -exec will be more efficient as it will use the minimum needed number of forks to chown instead of one fork per file/directory
  • SuperFamousGuy
    SuperFamousGuy over 9 years
    A+ worked like a charm for me.
  • R. van Twisk
    R. van Twisk over 7 years
    -h affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)
  • Pathros
    Pathros about 6 years
    I tried chown nginx:nginx -R /path/to/.[^.]* and it only changed ownership to .dot hidden files. not all.
  • klor
    klor almost 5 years
    This worked for me
  • god_is_love
    god_is_love almost 5 years
    @wfaulk As was mentioned by @Hamish Downer you must do both the * and follow it with .[.^]* to get all files.
  • Digger
    Digger almost 4 years
    Debian Jessie (chown v. 8.23). for chown -R user:group /home/user/*, I get chown: cannot access '/home/user/*': No such file or directory'
  • Hamish Downer
    Hamish Downer almost 4 years
    @Digger - the path (and user and group) are examples. You should use the actual path (and user) that you want to use.
  • TheArchitecta
    TheArchitecta about 3 years
    Risk on this one is if you are changing directories to make the change in various places you could inadvertantly execute this at root directory. I prefer to explicity change the path on the command to the directory in question rather than navigating there and running it directly.
  • usernotnull
    usernotnull over 2 years
    I wanted to include hidden dotfiles but '*' wasn't working... the '.' worked