How to chmod files only on Linux

Sometimes, you might want to apply a chmod to files only and not directories. This guide shows you three different ways to achieve that goal on the Linux command line.

How to chmod files only

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.

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

Chmod files only example

Chmod files recursively from any folder

In the first example, we had to enter the directory with cd command first, this can be omitted. You can specify the directory inside the find command instead of entering the directory first.

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

Alternative chmod files only example

The best performing chmod file option

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

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