Patrick Ward words, code, and music

Recursively chmod Only Directories or Only Files

I needed to chmod a deeply nested directory structure recently, but the directories needed to be 777 while the files needed to be set to a different level of permission. It’s trivial to chmod on a directory and all it’s files, but doing it only on the directories while leaving the files alone, required the use of the find command.

So, this is another note to self, for the next time I need to remember this.

Recursively change permissions on directories only:

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

Breaking it down:

  • Uses the find command on the current directory .
  • -type d tells find to only look for files of type directory
  • -exec tells find to perform the following action on each matching file. In this case it was to chmod each directory to 777.

This same technique can be used to chown directories and/or perform the same actions on only files.

Recursively change owner on directories only:

find . -type d -exec chown www-data.www-data {} \;

Recursively chmod/chown on files:

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