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.

79 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!!!

  38. Gravatar 38 Calan Is A Geek » Recursively chmod directories or files Says:

    [...] /dev/movabletripe » Recursively chmod directories only. Leave a comment | Trackback Mar 12th, 2010 | Posted in Unix Tags: chmod, only, recursively [...]

  39. Gravatar 39 $t4cK Says:

    Thanks man! Very good tip! :)

  40. Gravatar 40 Cambiar permisos recursivamente « Pa’ no olvidar Says:

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

  41. Gravatar 41 Thaddeus Says:

    Thanks. This is still paying dividends four years hence.

  42. Gravatar 42 laurent-rpnet Says:

    Thanks, very useful tip.
    But if you need to change only the x permission on directories – which is often the case – you can use chmod alone with X instead of x:
    chmod -R ug+X,o-X * will give execute permission for user and group and remove from others only on directories in the subtree * (and won’t change files, x or not x)

  43. Gravatar 43 chmod 755 rekursiv NUR auf Verzeichnisse | pregos blog Says:

    [...] (via) [...]

  44. Gravatar 44 Recursively Change File Permissions on Folders Only – Linux/Unix « Random Tech Solutions Says:

    [...] find . -type f -exec chmod 644 {} ; Solution Credit: MovableTripe [...]

  45. Gravatar 45 Ashish Says:

    Great thing thanx for sharing u r knowledge

  46. Gravatar 46 Ken Conoly Says:

    Very useful, surprised chmod doesn’t have a switch for +/- directories, but this is a great workaround.

  47. Gravatar 47 ArdianYS Free Prestashop Theme Says:

    thanks for the tip. using it on my prestashop tutorial to change folder and files permission. Regards.

  48. Gravatar 48 Yahoo News Says:

    Yahoo News…

    This is really good news today….

  49. Gravatar 49 Read More Here Says:

    Yahoo News…

    This is really good news today….

  50. Gravatar 50 Ali Says:

    greate !
    it’s very useful command for Server managers
    Thanks a lot

  51. Gravatar 51 Free Movie Online Says:

    Yahoo Movie…

    This is really great news today….

  52. Gravatar 52 leancode Says:

    Good stuff! But the command runs a little slow the way its run.
    I propose the following using xarg which does exactly the same thing but executes much quicker:

    find . -type d -print0 |xargs -0 chmod 740 # for all directories 740 starting at .
    find . -type f -print0 |xargs -0 chmod 640 # for all files 640 starting at .

  53. Gravatar 53 Chris P Says:

    Many thanks – saved a lot of extra, error-prove work…

  54. Gravatar 54 Sanjoy Roy Says:

    :) Saved my time. Really good job guys! Thanks a lot!!!

  55. Gravatar 55 Sorcix Says:

    Thanks! This really saved me loads of time. :)

  56. Gravatar 56 lciani Says:

    I’m grateful to the OP and to all the people that made comments and improvements.

    The variation posted by Ralph
    find . -type d -exec chmod 755 \"{}\" \;
    got stuck on a file named $29.00.mp3

    Any ideas on how to solve this?

    P.S. the version of leancode
    find . -type f -print0 |xargs -0 chmod 640
    does not work on my NAS which runs some kind of Linux and BusyBox v1.1.1 complains that the usage of find and of chmod is wrong

  57. Gravatar 57 Modificar permisos de ficheros recursivamente « El Blog de rubensa Says:

    [...] vía [...]

  58. Gravatar 58 danny Says:

    Thanks a lot! Bookmarked.

    I use this often after I download a WordPress plugin or theme. Out-of-the-box, most of them need file permissions adjusted to work.

  59. Gravatar 59 Géza Says:

    Thank you for this tip.

    My shell did not realised \; at the end, so I had to use ‘;’ instead:

    find . -type d -exec chmod 755 {} ‘;’

  60. Gravatar 60 Greg Says:

    The variation posted by Ralph
    find . -type d -exec chmod 755 \”{}\” \;
    got stuck on a file named $29.00.mp3

    Any ideas on how to solve this?

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

    $ says the next thing is a shell variable. “” allows parsing of shell variables, ” does not

  61. Gravatar 61 Cambiar permisos recursivamente | Pa' no olvidar Says:

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

  62. Gravatar 62 Unix: Securing a Web site with the proper file permissions « Technical Notebook Says:

    [...] to the Movable Tripe for providing the above [...]

  63. Gravatar 63 Chmod recursively directories and files | How to Says:

    [...] goes here. (function() {var s = document.createElement('SCRIPT'), s1 = [...]

  64. Gravatar 64 chmod recursively only files or only directories Says:

    [...] Credit: here. [...]

  65. Gravatar 65 Greg Says:

    Thanks, this post and the responses are very useful!

  66. Gravatar 66 Kevin van Zonneveld Says:

    I don’t think you need find to achieve this.


    chmod -R 644 ${dir}
    chmod -R +X ${dir}

    should do the trick as -X (capital x), only adds the execute flag to directories

  67. Gravatar 67 logofun Says:

    tanks alot :X

  68. Gravatar 68 sam Says:

    thanks is useful article

  69. Gravatar 69 Stuart Says:

    Cheers dude

  70. Gravatar 70 » chmod recursively – but folders only kleinhirn.blog Says:

    [...] the files inside? I did. I know I’ve had this problem about a hundred times, googled it, found a solution – and then forgot about it [...]

  71. Gravatar 71 Milin Mestry Says:

    Thanks a lot. It save lot of my time and hitting keyboard :-) . Thanks *nix family

  72. Gravatar 72 Chmod Recursive pada File dan Direktori « duken.info Says:

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

  73. Gravatar 73 Brad Markle Says:

    That did the trick, thanks!

  74. Gravatar 74 Pedro Rocha Says:

    Simple and objective! Great post. I made a portuguese version in my blog: http://pedrorocha.net/2011/10/mudar-permiss%C3%B5es-com-chmod-somente-em-arquivos-ou-somente-em-pastas

  75. Gravatar 75 Free runescape Says:

    Free runescape…

    [...]I do agree with all of the ideas you have introduced in your post. They are really convincing and will definitely work. Nonetheless, the posts are too brief for starters. May you please prolong them a bit from subsequent time? Thanks for the post….

  76. Gravatar 76 Jared Says:

    I’ll help some of you save your life. BE SURE to specify the file path of the initial directory because if you make a mistake you will literally CHMOD every single file or folder. I learned that the hard way by making the mistake of thinking I was using the command in the correct directory and I was wrong.

  77. Gravatar 77 chmod_fan Says:

    thank you for sharing, this was really useful!

  78. Gravatar 78 Asian Tiger Mosquito Says:

    2011…

    Thanks, I’ve recently been searching for info about this topic for ages and yours is the greatest I have discovered so far. But, what about the conclusion? Are you sure about the source?…

  79. Gravatar 79 dejadejoder » chmod fichier ou dossier uniquement Says:

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

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>