Wednesday, August 13, 2008

Change Permissions on All Directories

Sometimes I need to recursively set permissions on all directories in a file hierarchy. Other times, I just want to set file permissions while ignoring directories. Fortunately, the find command makes this easy.

# make all directories read, write, execute for owner. read, execute for group and other
find . -type d -exec chmod 755 {} \;

# make all files read, write for owner. read for group and other
find . -type f -exec chmod 644 {} \;

# make all shell scripts read, write, execute for owner. read, execute for group and other
find . -type f -name "*.sh" 755 {} \;

5 comments:

Steve Laniel said...

You want to use find + xargs rather than find + the -exec argument.

Note also that sometimes you need to descend into a directory before applying a command to the directory itself. It doesn't apply in this case, but it does in others. I, for instance, often need to do a bulk renaming operation; if I try to rename a directory before I rename the files it contains, badness ensues. In this case you use the '-depth' argument to find(1), and -execdir instead of -exec.

Nikola Valentinov Petrov said...

use chmod -R permission :)

Travis Whitton said...

Nikola, chmod -R doesn't make any distinction between files and directories. Otherwise, it's good for modifying lots of files en masse.

Stephane said...

You can use chmod u=rwX,g=rX,o=rX to achieve that effect: the X modifier gives the execution bit to directories or scripts which already are +x
Very handy ;)
http://linux.die.net/man/1/chmod

Anonymous said...

Thanks, buddy! I was having a mess of a time with my music library. Permissions on folders and files were all messed up. Fixed!