How to chmod files only on Linux

There are several ways to apply a chmod to files recursively on Linux. One of the easiest ways is to use the find command to select the files and then run the chmod command with the -exec switch. Change into the directory with cd, before you run the find command.

Chmod Example 1:

cd /var/www/mydirectory
find . -type f -exec chmod 750 {} +

Chmod files only example

You can also specify the directory inside the find command instead of entering the directory first.

Chmod Example 2:

find /var/www/mydirectory -type f -exec chmod 750 {} +

Alternative chmod files only example

Another way to achieve the same goal is to pipe the output of the find command to chmod by using the xargs command.

Chmod Example 3:

find /var/www/mydirectory -type f -print0 | xargs -0 chmod 750

Chmod using xargs

The command is a bit longer but it should provide a better performance when many files are involved.

Leave a Comment