45

I want to change permissions on a tree on Centos 4 to add execute permissions for all directories recursively from a directory. If I use normal chmod, files other than directories are also modified:

chmod -R o+x /my/path/here

How can I only affect directories?

Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311
WilliamKF
  • 8,058

3 Answers3

61

Run find on -type d (directories) with the -exec primary to perform the chmod only on folders:

find /your/path/here -type d -exec chmod o+x {} \;

To be sure it only performs it on desired objects, you can run just find /your/path/here -type d first; it will simply print out the directories it finds.

Daniel Beck
  • 111,893
26

See Command line examples - chmod in the Wikipedia.

# Remove the execute permission on all files in a directory 
# tree, while allowing for directory browsing.
chmod -R a-x+X directory

As added by Daniel: this should work in your case:

chmod -R o+X directory
1
find /home/mydir -type d | xargs chmod ugo+rx

This works on CentOS6, which the above find -exec does not. Basically it just pipes the list of directories to the xargs command which sends them to chmod. The chmod then sets universal read and execute (search) on the directories. To do this for all users in home use sudo:

sudo sh -c "find /home/ -type d | xargs chmod ugo+rx"