How can I make chown work recursively?

308

65

I've got a directory called pdfs that contains a bunch of sub- and sub-sub-directories. I want to change ownership on all PDF files in all of the subfolders. I just tried this:

chown -R someuser:somegroup *.pdf

...but it didn't change ownership of the PDFs in subdirectories. The user and group do exist.

According to the man page for chown, the -R should mean recursive:

-R, --recursive
          operate on files and directories recursively

What am I missing?

Nathan Long

Posted 2011-03-22T16:29:12.640

Reputation: 20 371

Answers

315

Recursive mode only works on directories, not files. By using the glob '*.pdf' the shell is passing the file list to chown, which sees these are files, and changes the permissions on the files it sees, and that's it.

Remember, in shells, the glob is evaluated by the shell, not the command. If the glob matches files, they are passed to the command and the command never knows a glob existed. (This is different than how Windows Command prompt used to do things). If you have a dir, with the contents something like:

machine:$ ls -F
file1.pdf  file2.pdf  other.txt  subdir/

And you typed:

chown -R someuser:somegroup *.pdf

The shell would first make the list: file1.pdf file2.pdf

and then run your command:

chown -R someuser:somegroup file1.pdf file2.pdf

See, there's no directory for -R to act on. It does what you asked it - change ownership on the two files on the command line, ignoring that quirky -R flag.

To do what you want, to use the '*.pdf' as a pattern for this directory and subdirectories, you can use find, which can find files that match a filename pattern (or many other criterea) and pass to a subcommand

find . -type f -name '*.pdf' | xargs chown someuser:somegroup

This starts in current dir '.' to look for files (filetype f) of name pattern '*.pdf' then passes to xargs, which constructs a command line to chmod. Notice the quotes around the pattern '*.pdf', remember that the shell will create a glob if it can, but you want the pattern passed to find, so you need to quote it.

Because filenames may have spaces in them, you want to use a trick to make it filename-with-spaces safe:

find . -type f -name '*.pdf' -print0 | xargs -0 chown someuser:somegroup

In bash 3 and lower, this is the way you need to do it. More powerful globbing is available in bash 4 (with shopt -s globstar)and other shells. The same in zsh, using a recursive glob **:

chown -R someuser:somegroup ./**/*.pdf

Rich Homolka

Posted 2011-03-22T16:29:12.640

Reputation: 27 121

@gwideman I know this is old... But yes, of course -R does recursive. The OP just had an issue with a very specific type of recursion, an extension and "file type is file" filtered one – Rich Homolka – 2015-03-07T23:40:41.787

@RichHomolka Ah. OP said the files were in a directory called 'pdfs', so I assumed the problem was in how to specify pdfs as the directory to recurse, and that all contained files were pdfs, so no need to select them specifically. But you may well be right, if the job is to select only pdf files, and leave others unchanged. – gwideman – 2015-03-08T23:51:48.433

1Edited to reflect that bash 4 with shopt -s globstar does recursive globbing. – kojiro – 2011-03-22T22:54:54.543

@kojiro thanks! as you can tell I still use bash3 – Rich Homolka – 2011-03-22T22:58:01.107

2Per the man page shown by the original poster, I found the chown -R did indeed change owner on folders AND files. No need for find. Using Mint 15. – gwideman – 2013-09-28T12:23:14.767

57

chown -R someuser:somegroup /your/folder/here/*

This will apply chown to all files and all subdirectories and sub-subdirectories of the specified folder. Use with care.

Sprachprofi

Posted 2011-03-22T16:29:12.640

Reputation: 671

2Somehow sudo chown -R user ./ worked for me, but sudo chown -R user ./* didn't – Slav – 2018-06-04T08:14:45.347

1I know this is old, but though this answers the headline, does not answer the question. This is actually slightly worse than what OP had already tried. – Rich Homolka – 2018-07-20T15:29:21.760

2@RichHomolka Having been brought here just because of the question title, I am quite happy to find this answer. – Félix Gagnon-Grenier – 2019-03-12T20:49:20.117

@FélixGagnon-Grenier fair enough. If this answer works for you I’m very happy. But it wouldn’t have worked for the original question. – Rich Homolka – 2019-03-15T17:22:20.493

This seems not to work for dot files such as .env. – Ryan – 2019-05-13T15:10:42.393

This worked for my hidden files ("dot files") – lobi – 2020-02-21T20:46:43.087

14

You can use the find utility:

find . -name '*.pdf' -exec chown someuser:somegroup {} +

Please don't forget the quotes around *.pdf. Otherwise the shell will try to expand it. This means already the shell will replace *.pdf with the names of all PDF files found in the current directory. But that's not what you want. You want to find the PDF files located in subdirectories. Btw.: That's also the problem with your chown command.

bmk

Posted 2011-03-22T16:29:12.640

Reputation: 1 735

@PatrickM one (minor) other advantage to + vs ; .... plus isn’t a shell meta character and doesn’t have to be escaped – Rich Homolka – 2018-07-20T14:36:35.253

1I had to look up the +, neat trick for performance. -exec command {} + This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines. Only one instance of {} is allowed within the command. The command is executed in the starting directory. – Patrick M – 2014-01-13T18:28:45.363

6

The command

chown -R someuser:somegroup *.pdf

will only recurse down directories if the directory name ends in .pdf. You need something like:

find . -name "*.pdf" -exec chown someuser:somegroup {} \;

Mike Scott

Posted 2011-03-22T16:29:12.640

Reputation: 4 220

Technically it will only go to one level down. whether you call that true recursion or not is an exercise for the user :) – Rich Homolka – 2015-04-06T21:28:13.807

1

to change the ownership of a directory recursively simply use:

sudo chown -R <username>:<groupname> <dir name>

here username = the new user who should be owner of directory

groupname = the new group which should be owner of directory

every file/directory has a user owner and a group owner

KawaiKx

Posted 2011-03-22T16:29:12.640

Reputation: 813

This does not do what the OP asked. This changes ownership of everything, OP asked for a specific set of files. – Rich Homolka – 2017-01-15T18:50:56.443

0

I use tree instead:

 sudo tree -fai ~/.blabla  | xargs -L1 -I{} sudo chown youruser:youruser {}

Also take care to not run recursive chown or chmod on '/' directory or other system directory.

Eduard Florinescu

Posted 2011-03-22T16:29:12.640

Reputation: 2 116

0

To become the owner of all files in a directory, use

find directory -type f -name '*' | sudo xargs -d '\n' chown $USER

instead of

sudo chown $USER directory\*

or

sudo chown --recursive $USER directory

which might not work if there are too many arguments produced by * (too many files in the directory) or which does not not do what you want respectively.

masterxilo

Posted 2011-03-22T16:29:12.640

Reputation: 333