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
Re: CHMOD takes a Long timeIt's the number of files affected rather than the total size - chmod on one 36GB file will need to change the attributes of one directory entry, chmod on 36 billion 1 byte files will take 36 billion times as long as it will need to read each directory entry, modify it then write it back.
The chmod command with the -R options allows you to recursively change the file's permissions. To recursively set permissions of files based on their type, use chmod in combination with the find command. If you have any questions or feedback, feel free to leave a comment.
To modify the permission flags on existing files and directories, use the chmod command ("change mode"). It can be used for individual files or it can be run recursively with the -R option to change permissions for all of the subdirectories and files within a directory.
The command chmod -R 777 / makes every single file on the system under / (root) have rwxrwxrwx permissions. This is equivalent to allowing ALL users read/write/execute permissions.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With