8

How to change all file permissions to 644 and all folder permissions to 755 recursively using chmod in the following two situation:

  • If they had 777 permissions
  • Regardless of the permission (with ANY permissions)
smhnaji
  • 609
  • 2
  • 11
  • 24

3 Answers3

26

find . -type d -perm 777 -exec chmod 755 {} \; (for changing the directory permission)

find . -type f -perm 777 -exec chmod 644 {} \; (for changing the file permission)

If the files/directories dont have 777 permissions, we easily remove the -perm 777 part. The advantage of these commands is that they can target regular files or directories and only apply the chmod to the entries matching a specific permission.

. is the directory to start searching

-type d is to match directories (-type f to match regular files)

-perm 777 to match files with 777 permissions (allowed for read, write and exec for user, group and everyone)

-exec chmod 755 {} \; for each matching file execute the command chmod 755 {} where {} will be replaced by the path of the file. The ; indicates the end of the command, parameters after this ; are treated as find parameters. We have to escape it with \ since ; is the default shell delimiter, it would mean the end of the find command otherwise.

JulienCC
  • 123
  • 3
smhnaji
  • 609
  • 2
  • 11
  • 24
13

Regardless of the permissions:

chmod -R a=r,a+X,u+w /your/path
adaptr
  • 16,479
  • 21
  • 33
  • How does this work? – Jimbali Apr 20 '16 at 10:41
  • according to man chmod: The letters rwxXst select file mode bits for the affected users: read (r), write (w), execute (or search for directories) (x), execute/search only if the file is a directory or already has execute permission for some user (X) ... – mrossi Jun 19 '17 at 13:44
  • +1, this is way much faster than the accepted answers if you have thousands of files – the_nuts Jun 23 '17 at 16:26
  • Excellent! This inspired a one liner to reset permissions to umask `chmod -R "$(umask -S | sed 's/x/X/g')" /your/path` – Steven Shaw Jan 30 '20 at 22:50
  • 1
    @Jimbali It works as follows: `all=set to readonly,all=add execute if folder,owner=add write` ... And it is INFINITELY better than all the stupid `find` answers. Find has to launch a chmod process for every file/folder. @adaptr 's answer only does a single, efficient chmod process. – Mitch McMabers Jan 28 '22 at 16:04
-3
sudo find /path/to/someDirectory -type f -print0 | xargs -0 sudo chmod 644

and

sudo find /path/to/someDirectory -type d -print0 | xargs -0 sudo chmod 755
rajith
  • 1
  • How is this better than the accepted answer? – kasperd Nov 27 '15 at 09:00
  • @kasperd It's much worse, because it sends all filenames as arguments all at the same time. This can lead to too many arguments on the line. **THE ONLY GOOD ANSWER** in this whole question is by adaptr who does it properly with just `chmod` itself. – Mitch McMabers Jan 28 '22 at 16:53