Xargs + chmod – file or directory not found

chmodfindpermissionspipexargs

I am trying to recursively change the permission of all files and directories in my project.

I found a post on the magento forum saying that I can use these commands:

find ./ -type f | xargs chmod 644
find ./ -type d | xargs chmod 755
chmod -Rf 777 var
chmod -Rf 777 media

It worked for find ./ -type d | xargs chmod 755.

The command find ./ -type f returned a lot of files, but I get chmod: access to 'fileXY.html' not possible: file or directory not found on all files, if I execute find ./ -type f | xargs chmod 644.

How can I solve this?

PS: I know that he recommended to use 777 permission for my var and media folder, which is a security risk, but what else should we use?

Best Answer

I’m guessing you’re running into files whose names contain characters which cause xargs to split them up, e.g. whitespace. To resolve that, assuming you’re using a version of find and xargs which support the appropriate options (which originated in the GNU variants, and aren’t specified by POSIX), you should use the following commands instead:

find . -type f -print0 | xargs -0 chmod 644
find . -type d -print0 | xargs -0 chmod 755

or better yet,

chmod -R a-x,a=rX,u+w .

which has the advantages of being shorter, using only one process, and being supported by POSIX chmod (see this answer for details).

Your question on media and var is rather broad, if you’re interested in specific answers I suggest you ask it as a separate question with more information on what your use of the directories is.

Related Question