Recursive Directories/Files Only

November 22, 2009 by: Allen Sanford

Have you ever wanted to easily and recursively chmod, chown, or any other command and only have the command execute only on directories or files. Well I am going to show you just how to accomplish this right now.

For the below examples I am going to do chmod but it could easily be any other command.

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

Short explanation is this will recursively search your directory tree (starting at dir “.”) and chmod 755 all directories only. Pretty simple!

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

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

You should have picked up and what is happing here by now the find command also use to use the -exec flag to then apply a command to results of the find. In the find command we start at the current directory and the use the -type flag to tell it what type to find. You can get inventive and use the find command in a ton of other ways and get results that are similar but tailored to your scenario.

Enjoy and Have a Good’n!

linux-logo

Filed under: Linux
Tags: , ,

Comments

One Response to “Recursive Directories/Files Only”

Leave a Reply