I want to change permissions of all 777 folders to 755 and also change all 777 php files to 644.

How can I do this through shell?

P.S: all files and directories are in www directory.


You can change the permissions of all 777 folders to 755 using find as below.

find /var/www -type d -perm 777 -print -exec chmod 755 {} \;

The above command will change all the directories inside /var/www to have the permission set as 755. To verify it, you can use the below command.

stat -c "%a %n" /var/www/directory-name

To change the permissions of all php files, you can use the below command.

find /var/www/some-directory -type f -name "*.php" -perm 777 -print -exec chmod 644 {} \;

Again, you can use the stat command to verify if the permissions had changed. Or you can even use,

ls -ld /var/www/some-directory-name

Both stat and ls -ld will display the octal permissions of the file.

  • 1
    What is the -print doing there?
    – terdon
    Apr 28 '14 at 14:56
  • 2
    @terdon, I just thought may be we can verify which directory's permissions actually got changed :)
    – Ramesh
    Apr 28 '14 at 15:18
  • Ah, indeed, fair point.
    – terdon
    Apr 28 '14 at 15:27

"www" could be anywhere on your system, so be more specific next time.

Anyway, I am assuming you meant /var/www:

find /var/www -type d -perm 777 -print0 | xargs chmod 755
find /var/www -name "*.php" -perm 777 -print0 | xargs chmod 644

In the future, refer to man find. It's quite powerful, as you can gather.

  • 1
    Bear in mind that this will break on some file names, use -print0 for find and xargs -0 to avoid that.
    – terdon
    Apr 28 '14 at 14:56
  • @terdon: should we use them together? xargs -0 will use null for termination, while -print0 will not echo null in output. Apr 28 '14 at 15:22
  • @MohammadEtemaddar yes it will, -print0 uses null as output separator and -0 tells xargs to use null as input separator.
    – terdon
    Apr 28 '14 at 15:28

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.