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
@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