How to Chown a directory recursively including hidden files or directories
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
Related videos on Youtube
user4723402
Some favorite languages: Javascript, Python, Ruby, Haskell, ActionScript.
Updated on September 17, 2022Comments
-
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 about 12 yearsDoing 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 about 12 yearsThis 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 about 12 yearsnote 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 over 9 yearsA+ worked like a charm for me.
-
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 about 6 yearsI tried
chown nginx:nginx -R /path/to/.[^.]*
and it only changed ownership to .dot hidden files. not all. -
klor almost 5 yearsThis worked for me
-
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 almost 4 yearsDebian Jessie (chown v. 8.23). for
chown -R user:group /home/user/*
, I getchown: cannot access '/home/user/*': No such file or directory'
-
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 about 3 yearsRisk 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 over 2 yearsI wanted to include hidden dotfiles but '*' wasn't working... the '.' worked