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.

37 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 Ralph Says:

    really useful although I had a problem with directories and files that had spaces and ‘ in them .

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

  21. Gravatar 21 Bob Blackwell Says:

    An excellent find!!

    Exactly what I needed to resolve a self imposed problem. Thank you

  22. Gravatar 22 javier Says:

    Just what I was looking for.

    Thaks a lot.

    javier.

  23. Gravatar 23 Vloris Says:

    You can solve the problem with spaces in filenames by using xargs as follows:
    find . -type f -print0 | xargs -0 chmod 644
    This has one additional benefit: you are not calling chmod for every file, xargs takes care to put as many filenames on the command line of a single xargs invocation as possible.
    The -print0 and -0 arguments make sure the filelist passed from find to xargs contain null-terminated filenames. Xargs takes these and makes sure one filename is a single argument to chmod.

  24. Gravatar 24 Niels Says:

    thanks a lot, after a first meeting with chmod, i screwed up already, this way i could retain rights of my webpage.

  25. Gravatar 25 Hasan Says:

    thanks everyone for the recursively chmod tips.

  26. Gravatar 26 Recursively chmod directories only » Go Web Young Man Says:

    [...] – Warning: This post is for geeks only! Movabletripe’s post explaining how to recursively chmod only directories has been a helpful reference to me in recent months. However, today I tried to access the site, and [...]

  27. Gravatar 27 Not quite fluent with Linux yet - dirkraft’s texlog Says:

    [...] (src: Recursively chmod directories only) [...]

  28. Gravatar 28 Finally Says:

    Just what I was looking for to solve crappy Perforce problems, need directories to be writeable and files to be read-only….piece of crap version control

  29. Gravatar 29 Bogdan Says:

    Thanks a lot for this!

    Your post is the oldest among the similar, so I guess you deserve all the credit :)

  30. Gravatar 30 Best method to recursively chmod/process files or directories Says:

    [...] 8th June 2009 Found here. [...]

  31. Gravatar 31 skent Says:

    thanks! extended to a useful script that sets my scripts permissions correctly too:

    #!/bin/sh
    find . -type d -exec chmod 755 {} \;
    find . -type f -exec chmod 644 {} \;
    find . -type f -name \*.sh -exec chmod 744 {} \;

  32. Gravatar 32 Ray Says:

    Sweet,… thanks Mr. Hennessy,…

  33. Gravatar 33 Robert Says:

    Thanks! A great help!

  34. Gravatar 34 Recursively chmod directories only « :maohao: Says:

    [...] Recursively chmod directories only By maohao Here. [...]

  35. Gravatar 35 Guido Says:

    just thanks! simple, short and so powerful!!!

  36. Gravatar 36 Fran Says:

    3 years on and still helping – nice one and thanks!

  37. Gravatar 37 Adam Says:

    Man!! Thank you!!! I did a chomd 777 -R to my Docs folder. Man the funky highlighing was annoying! Thanks again!!!

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>