Run Chmod separately for files and directories

If you are a Linux user, you have probably heard about the console app chmod. It is a useful tool which allows changing file system permissions using a terminal session or a terminal emulator. When you run it, it changes the permissions that you specify on files and directories all at once. But you might need to change file permissions separately from directory permissions and vice versa. Here is how it can be done.
chmod logo banner

There are several ways to do it, but my favorite and most reliable way to do it is to combine chmod with xrags and find tools. The following command will modify the permissions for files only.

find /path/to/location -type f -print0|xargs -0 chmod 644

Here the find command lists all the files in the specified location. The -type f switch tells it to list only files and exclude directories. The switch -print0 allows processing filenames in a way such that file or directory names containing spaces or new lines are correctly handled.

A similar appropriate command can be used for directories too. The only portion you need to change is -type f to -type d:

find /path/to/location -type d -print0 |xargs -0 chmod 755

Tip: You can save your time and type less by using this trick.

  1. First, apply file system permissions to files and folder by running chmod in recursive mode:
    chmod -R 644 /path/to/location

    It will apply rw-r-r permissions to all files and folders in the specified location.

  2. Next, execute the command for directories only:
    find /path/to/location -type d -print0 |xargs -0 chmod 755

That's it.

Support us

Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:

If you like this article, please share it using the buttons below. It won't take a lot from you, but it will help us grow. Thanks for your support!

Author: Sergey Tkachenko

Sergey Tkachenko is a software developer who started Winaero back in 2011. On this blog, Sergey is writing about everything connected to Microsoft, Windows and popular software. Follow him on Telegram, Twitter, and YouTube.

Leave a Reply

Your email address will not be published.

Exit mobile version
Using Telegram? Subscribe to the blog channel!
Hello. Add your message here.