1

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?

4

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.

  • Or execute chmod from find directly with -exec chmod 644 {} +. – Kusalananda Jun 1 '18 at 14:06
  • Since the question isn't tagged with the Linux tag, in the interest of not confusing non-Linux users who find this page I'd note that print0 is a GNU platform-specific extension and not available to the POSIX standard find command. LIkewise with the -0 option to xargs. – Andrew Henle Jun 3 '18 at 12:17
  • 1
    @Andrew done; note that the chmod standalone variant is POSIX-compliant (but I’m not sure it’s supported on macOS, despite the latter being certified Unix). – Stephen Kitt Jun 3 '18 at 12:37
2

How about:

find ./ -type d -exec chmod 755 {} +

this is the canonical portable solution that deals even with newlines in file names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.