Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

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.

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

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

share|improve this answer
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 at 14:56
    
@terdon: should we use them together? xargs -0 will use null for termination, while -print0 will not echo null in output. –  Mohammad Etemaddar Apr 28 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 at 15:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.