Skip to main content

Recursively change permission of files or directories only (*nix)

2 min read

We usually use chmod to change the permission of a file and or folder on Linux/Mac. When we chmod a directory recursively it also changes it's subdirectories and the files inside it to same permission.

The issue arises because we may not need the same permission for the files as like the directories. Take a website as example. Generally website files have 644 permission and directories have 755 permission.

If you have a little number of files and directories then it's okay for you to change the permissions manually per file or directory. But what about 1000s files and directories? What will you do then?

Hold on man, we are *nix users. We must have a solution!

And yes, we have a great tool called find. You can easily look for the files or folders recursively and change the permission at a rapid mode with this tools.

I use this command to change the permission of directory only and recursively.

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

And this command for changing the permission of files only and obviously recursively.

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

You may do the tweaks as needed.

Not only this, you can do a lot of other things with the combination of find and xargs. Find the possibilities of find with the commands below:

man find
man xargs