Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Does anyone have a tool or script that will recursively correct the file permissions on a directory?

On an Ubuntu Linux machine, a bunch of files were copied to a USB disk with full 777 permissions (user, group, other - read, write, execute) in error. I want to put them back in the user's directory corrected.

Directories should be 775 and all other files can be 664. All the files are images, documents or MP3s, so none of them need to be executable. If the directory bit is set then it needs execution, other wise it just needs user and group, read and write.

I figured it was worth checking if such a utility exists before hacking together a shell script :)

share|improve this question

4 Answers 4

up vote 22 down vote accepted

This should do the trick:

find /home/user -type d -print0 | xargs -0 chmod 0775
find /home/user -type f -print0 | xargs -0 chmod 0664
share|improve this answer
    
+1 for using -print0/xargs -0 :-). –  sleske Dec 1 '09 at 11:38

find can do the trick alone with -exec:

find /home/user -type f -exec chmod 0664 {} \;
find /home/user -type d -exec chmod 0775 {} \;
share|improve this answer
5  
Slower than using xargs, as you're forking a chmod for every file, where as xargs will run a chmod process with as many files as it can fit on a command line. On a very large tree, this can make a fair difference. –  David Pashley Jul 2 '09 at 15:43
    
thanks, you are absolutly right. +1 for freiheit. –  ThorstenS Jul 2 '09 at 15:49
1  
Also, print0/xargs -0 will handle even very weird filenames; not so sure about find -exec. –  sleske Dec 1 '09 at 11:38

This answer won't solve your problem, but someone might find it useful for a similar problem where files have less permission than they should do.

# chmod -R . u=rwX,g=rX,o=rX

The magic is the X permission, rather than x. The chmod manpage describes it thus:

execute/search only if the file is a directory or already has execute permission for some user

This isn't suitable in your case as your files have execute permission so, will match the second test.

share|improve this answer

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.