Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Like the way You ask. I found answer to my question just in Your question :) +1 – Line Aug 3 '14 at 13:47
up vote 147 down vote accepted

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
share|improve this answer
Thanks for the thorough explanation! – Nathan Long Mar 22 '11 at 17:07
@nathan and thanks for fixing my typos – Rich Homolka Mar 22 '11 at 18:31
Edited to reflect that bash 4 with shopt -s globstar does recursive globbing. – kojiro Mar 22 '11 at 22:54
@kojiro thanks! as you can tell I still use bash3 – Rich Homolka Mar 22 '11 at 22:58
Per 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 Sep 28 '13 at 12:23

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.

share|improve this answer
I 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 Jan 13 '14 at 18:28

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 {} \;
share|improve this answer
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 Apr 6 '15 at 21:28

It's not complicated.

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.

share|improve this answer
Clearly someone answer it since 4 years, but ty for the warning. – Francisco Tapia Nov 9 '15 at 17:22
This question is currently #1 on Google, so it's important to note the correct answer. – Sprachprofi Nov 10 '15 at 10:12
sure, the accepted one is more elegant, readable and complete – Francisco Tapia Nov 10 '15 at 11:59

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

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .