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)

3 Answers 3

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.

2
  • How does this work? What are the curly brackets and backslash for?
    – Jimbali
    Apr 20, 2016 at 11:02
  • Why (??) this is better tham chmod -R a=r,a+X,u+w /your/path ? Sep 10, 2018 at 0:08
13

Regardless of the permissions:

chmod -R a=r,a+X,u+w /your/path
5
  • How does this work?
    – Jimbali
    Apr 20, 2016 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, 2017 at 13:44
  • +1, this is way much faster than the accepted answers if you have thousands of files
    – the_nuts
    Jun 23, 2017 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 Jan 30, 2020 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. Jan 28 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
2
  • How is this better than the accepted answer?
    – kasperd
    Nov 27, 2015 at 9: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. Jan 28 at 16:53

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.