2

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.

2 Answers 2

5

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.

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

"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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .