The Computer Oracle

How can I make chown work recursively?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: City Beneath the Waves Looping

--

Chapters
00:00 Question
00:46 Accepted answer (Score 386)
03:09 Answer 2 (Score 87)
03:27 Answer 3 (Score 16)
04:07 Answer 4 (Score 6)
04:27 Thank you

--

Full question
https://superuser.com/questions/260925/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#unix #permissions

#avk47



ACCEPTED ANSWER

Score 420


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



ANSWER 2

Score 107


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.




ANSWER 3

Score 17


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.




ANSWER 4

Score 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 {} \;