How can I do a recursive chmod only on directories?

32

6

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?

WilliamKF

Posted 2012-07-28T18:19:14.570

Reputation: 6 916

Answers

49

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

Posted 2012-07-28T18:19:14.570

Reputation: 98 421

Could you explain what {} ; does? – Srekel – 2017-12-20T12:34:27.480

2

@Srekel see this answer https://askubuntu.com/questions/339015/what-does-mean-in-a-linux-command

– MADforFUNandHappy – 2017-12-31T10:20:05.697

17

See Command line examples - chmod in the Wikipedia.

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

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

chmod -R o+X directory

user118305

Posted 2012-07-28T18:19:14.570

Reputation:

1For those wondering about the difference, like me, it is that X will also apply execute permissions to any file which already has at least one execute permission bit already set (either user, group or other). In the general case the accepted answer is better. – ixe013 – 2014-11-27T18:23:49.353

This would affect the current permissions of files within directories. – scriptmonster – 2012-07-29T15:27:58.470

@scriptmonster the line quoted is wrong for this case, but chmod -R o+X directory should work for the OP. – Daniel Beck – 2012-07-29T15:36:16.393

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"

Mark White

Posted 2012-07-28T18:19:14.570

Reputation: 11