4

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?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Black
  • 1,989
  • 7
  • 28
  • 58

2 Answers2

8

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.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • Or execute `chmod` from `find` directly with `-exec chmod 644 {} +`. – Kusalananda Jun 01 '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](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html). LIkewise with the `-0` option to [xargs](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/xargs.html). – Andrew Henle Jun 03 '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 03 '18 at 12:37
3

How about:

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

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

schily
  • 18,806
  • 5
  • 38
  • 60