Recursively chmod directories only

June 19th, 2006

find . -type d -exec chmod 755 {} \;

This will recursively search your directory tree (starting at dir ‘dot’) and chmod 755 all directories only.

Similarly, the following will chmod all files only (and ignore the directories):

find . -type f -exec chmod 644 {} \;

This entry was posted on Monday, June 19th, 2006 at 6:24 pm and is filed under Linux. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

21 Responses to 'Recursively chmod directories only'

  1. Gravatar 1 Tim Says:

    Thanks!!! It’s amazing how the simplest things can drive you insane.

  2. Gravatar 2 Ernesto Says:

    This was extremely helpful for me too, so thanks for the tip. Exactly what I was looking for.

    Now, if you could explain the syntax a little bit in order for us to be able to invent similar constructs? I am trying to understand exactly everything after the 644, that is

    {} \;.

    What does this stand for?

  3. Gravatar 3 Joel Says:

    Ernesto:

    It’s all in man find. The {} gets replaced by the current file find is processing (which is why you don’t need to use chmod -R), and \; is just there to mark the end of the exec expression.

  4. Gravatar 4 shilpa Says:

    This is simply wonderful. Look at my stupidity —
    >chmod -Rf ./*.java ./**/*.java ./**/**/*.java ./**/**/**/*.java …………………… :-)

  5. Gravatar 5 crystalsinger Says:

    Great post - have clipped it for future reference! :-)

    I also discovered that you can do (e.g.)

    find . -type f -name '*.htm*' -exec chmod 644 {} \;

    to only apply chmod to files with names matching a specified pattern. Great when you need to clean up after a php application install and set php files to 755, html files to 644, etc…. :-)

  6. Gravatar 6 huuan Says:

    thanks 8-) very cool, you rule!
    to restrict to an owner you can also do:
    find . -type d -user fileowner -exec chmod 0755 {} \;

  7. Gravatar 7 Wes Says:

    This just saved me one huge headache, shows what can be done if you think out of the box..

  8. Gravatar 8 Ron Says:

    Wow, thanks all.. *nix is so f-king awesome.

    Powerful.. Try doing a rename/repermission like this in windows…. (or dos)

    Ron

  9. Gravatar 9 Rocket Monkey Rodeo » Blog Archive » Recursively chmod Says:

    [...] /dev/movabletripe » Recursively chmod directories only [...]

  10. Gravatar 10 Data Access Notes » Recursively chmod directories only Says:

    [...] source: http://movabletripe.com/archive/recursively-chmod-directories-only/ At this source, there is discussion and additional find commands from responders. [...]

  11. Gravatar 11 Eugen Pyvovarov Says:

    Рекурсивна зміна параметрів доступів тільки для файлів чи директорій…

    Утилітки chmod та chown не вміють змінювати параметри доступу тільки для файлів, або тільки для директорій. Знайшов цікавий приклад, як таке мо…

  12. Gravatar 12 Michal Iglewski Says:

    Thanks!!!

    It will also work for .files

  13. Gravatar 13 thehenrys.net » Recursively chmod directories or files only Says:

    [...] http://movabletripe.com/archive/recursively-chmod-directories-only/ [...]

  14. Gravatar 14 Yep! Says:

    Perfect, it solved my problem instantly!

  15. Gravatar 15 Allen Halsey Says:

    Thanks for the tip! Reading the man page for chmod, I came across another tip for recursive chmod. I often encounter the situation where the ‘user-owner’ permissions are correct, but the ‘other’ permissions have been made too restrictive, disallowing read access. The following command will fix it:

    chmod -R o+rX .

    Note that the capital ‘X’ causes it to do the right thing regarding the executable bit: directories will be executable and files will be made executable only if the file is executable for the ‘user-owner’ or ‘group-owner’. Check out the man page for chmod.

    BTW, I always like to say ‘user-owner’, ‘group-owner’, and ‘other’, to help me keep straight that ‘u’ is for ‘user-owner’ and ‘o’ is for ‘other’.

  16. Gravatar 16 Justin Says:

    Another helpful command for changing files of only a specific type/extension is:

    find ./ -name *.pdf -exec chmod 755 {} \;

  17. Gravatar 17 Daniel Says:

    Awesome! Thanks for the tip, exactly what I was looking for.

  18. Gravatar 18 Charles Says:

    Take not to do something silly when mass changing files. I untarred a tarball, and blithely (blindly!) changed files to 644. Of course I needed many configure files to be executable. They weren’t. the build broke. Silly me!

    Otherwise, this is what I needed.

  19. Gravatar 19 Garçon aka Martin Kopta » Blog Archive » links for 2008-06-07 Says:

    [...] /dev/movabletripe » Recursively chmod directories only Recursively chmod directories only June 19th, 2006 find . -type d -exec chmod 755 {} ; This will recursively search your directory tree (starting at dir ‘dot’) and chmod 755 all directories only. Similarly, the following will chmod all files only (tags: commandline chmod) [...]

  20. Gravatar 20 Recursively chmod only directories or files « Letters to RainSoft Development Blog Says:

    [...] Luckily, I came across a post on movabletripe that dealt with the problem, as well as having some additional find snippets in the comments. The original article is here: Recursively chmod directories only. [...]

  21. Gravatar 21 home insurance phoenix Says:

    home insurance phoenix…

    action Luxembourg:radian blind steamboat vignettes …

Leave a Reply

XHTML: Permitted tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>