List too long to chmod recursively

William picture William · Jun 19, 2013 · Viewed 13.2k times · Source

I have tried the following command to chmod many images within a folder...

chown -R apache:apache *

But i get the follwing error

-bash: /usr/bin: Argument list too long 

I then tried ...

ls | xargs chown -R apache:apache *

and then get the following message...

-bash: /usr/bin/xargs: Argument list too long 

Does anyone have any way to do this? I'm stumped :(

Many thanks

William

Answer

Aaron Digulla picture Aaron Digulla · Jun 19, 2013

Omit the * after xargs chown because it will try to add the list of all file names twice twice (once from ls and then again from *).

Try

chown -R apache:apache .

This changes the current folder (.) and everything in it and always works. If you need different permissions for the folder itself, write them down and restore them afterwards using chown without -R.

If you really want to process only the contents of the folder, this will work:

find . -maxdepth 1 -not -name "." -print0 | xargs --null chown -R apache:apache