50

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

toby
  • 601
  • 1
  • 5
  • 6

9 Answers9

71

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.

Hamish Downer
  • 9,142
  • 6
  • 36
  • 49
  • 3
    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 May 10 '12 at 22:44
  • A+ worked like a charm for me. – SuperFamousGuy Feb 05 '15 at 00:11
  • I tried `chown nginx:nginx -R /path/to/.[^.]*` and it only changed ownership to .dot hidden files. not all. – Pathros Jun 23 '18 at 17:35
  • 1
    @wfaulk As was mentioned by @Hamish Downer you must do both the `*` and follow it with `.[.^]*` to get all files. – god_is_love Aug 23 '19 at 07:15
  • 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'` – Digger Aug 26 '20 at 04:31
  • @Digger - the path (and user and group) are examples. You should use the actual path (and user) that you want to use. – Hamish Downer Aug 27 '20 at 08:55
12

i believe the following command should work for this

chown -hR userid:usergroup /nameofdirectory/nameofsubdir/
hayalci
  • 3,611
  • 3
  • 25
  • 37
MattrixHax
  • 141
  • 1
  • 3
  • 2
    -h affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink) – R. van Twisk Feb 24 '17 at 10:29
8

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

 find /path/to/dir -exec chown USER {} \;
hayalci
  • 3,611
  • 3
  • 25
  • 37
  • 6
    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 – stew May 14 '12 at 02:31
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.

Craig
  • 289
  • 2
  • 7
  • 2
    With the side effect that you change the permissions on the current directory as well as all of its contents, which may or may not be what you want. – wfaulk May 10 '12 at 22:39
  • 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. – TheArchitecta Jun 23 '21 at 09:14
4

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

stdcall
  • 187
  • 1
  • 8
3

chown will work with hidden files and directories. In the following example, we will change user and group ownership for all files in ~/some/folder. All files includes all hidden files (e.g. .bashrc,.profile etc.) and folders at the ~/some/folder level and below. Note in particular that we do not wish to change ownership of ~/some, and so we will exclude the file ~/some/.. from the ownership changes.

$ cd ~/some/folder 
$ sudo chown -R usrname:grpname . 
$ 
Seamus
  • 266
  • 1
  • 6
2

Using for-loop with ls -A option, We can find all hidden files and directory exclude . and .. and then change the ownership for all hidden files and directory.

for i in `ls -A | grep "^\."`;do chown -R user:group $i;done

Use xargs option with ls -A

ls -A | grep "^\." | xargs chown user:group

For More details Click Here and Visit my Site

0

To chown ALL files in current directory and subdirectories for current user;

find . -exec chown $(whoami) {} \;

or if user can't chown some files due to restricted permissions;

sudo find . -exec chown $(logname) {} \;
5p0ng3b0b
  • 101
  • 2
-2

You could do something like

for i in `ls -A`;do chown -R user:group $i;done

The -A (capital A) is important as it excludes '.' and '..'

wfaulk
  • 6,828
  • 7
  • 45
  • 75
Zypher
  • 36,995
  • 5
  • 52
  • 95
  • 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. – wfaulk May 10 '12 at 22:48