How to apply recursively chmod directories without affecting files?

8,261

Solution 1

You can use find.

find ./ -type d -execdir chmod 750 {} +

Where 750 is the mode you'd like to apply and "./" is the directory you will recursively search.

EDIT: Thanks to @Gilles and find(1), I've revised this for additional security and performance.

Solution 2

In this particular case you can use X instead of x which only adds the bits to directories or files which already have the executable bit set for some user (i.e. chmod -R +X my_dir).

In general (e.g. if you wanted to make all directories readable without affecting the files), you could either use find with -type d or if you're using zsh (or bash 4 with shopt -s globstar) the **/ glob (both without passing the -R flag to chmod).

Share:
8,261

Related videos on Youtube

Ivan
Author by

Ivan

Currently I live in Prague, CZ, use Arch Linux on my Toshiba L10 (Centrino "Dothan" 1.6 Mhz) laptop and code (am beginning, actually) Scala 2.8 with NetBeans 6.9. I like Scala very much (finally, the language I really like) and wouldn't mind to get a jr. Scala developer position.

Updated on September 17, 2022

Comments

  • Ivan
    Ivan 9 months

    After I apply chmod -R to a directory, permissions are changed for everything within (files and directories). How can I add execute/search (x) permissions to directories without modifying the files?

  • sepp2k
    sepp2k over 12 years
    @Gilles: Good point, I've added that to the answer.
  • ewindisch
    ewindisch over 12 years
    @Gilles Thanks, it isn't too frequent that I learn something new about Unix tools :-) That is awesome. Also, now reading the find documentation, one should really use -execdir instead.
  • Ivan
    Ivan over 12 years
    "use X instead of x which only adds the bits to directories or files which already have the executable bit set for some user" - Thanks! I've missed this thing a lot!