Question

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

Was it helpful?

Solution

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

OTHER TIPS

You can simply pass the current directory to chown -R:

chown -R apache:apache .

The one corner case where this is incorrect is if you want all files and subdirectories, but not the current directory and the .dotfiles in it, to have the new owner. The rest of this answer explains approaches for that scenario in more detail, but if you don't need that, you can stop reading here.

If you have root or equivalent privileges, doing a cleanup back to the original owner without -R is probably acceptable; or you can fix the xargs to avoid the pesky, buggy ls and take out the incorrect final * argument from the OP's attempt:

printf '%s\0' * | xargs -r0 chmod -R apache:apache

Notice the GNU extension to use a null byte as separator. If you don't have -0 in your xargs, maybe revert to find, as suggested already in an older answer.

find . -maxdepth 1 -name '*' -exec chmod -R apache:apache {} +

If your find doesn't understand -exec ... {} + try -exec ... {} \; instead.

This may work:

find . -maxdepth 1 -not -name -exec chown -R apache:apache {} \;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top