How to Recursively Change File Permissions in Linux

Linux File Permissions Featured

Because Linux is a multi-user operating system, it has a mechanism that sets and manages file permissions to ensure that only authorized processes and users can access various directories and files. As you use Linux, you may encounter various instances where you can’t edit files or directories because of the “Permission denied” error, indicating you do not have the required privileges. This tutorial will show you how to recursively change file permissions in Linux to ensure that your permission settings apply to sub-folders and files.

How to check file permission(s) in Linux

When the “permission denied” error occurs on a file or a directory operation, start by checking the allocated permissions for the user, group and other.

To do that, you can use the ls -l command. For example, the following are permissions for various files and directories in the “/var” directory.

Ls L

The output above contains specific information.

  1. The first column represents the file and directory permissions. The first letter in this column indicates the type: d means directory, l stands for a symbolic link, and - a regular file.
  2. The other nine characters are grouped into three sets: u user, g group, and o owner indicate the file or directory permissions. r indicates “read” permissions, w indicates “write” permissions, and X indicates “execute” permissions.
Ls La Hightlight

Once you know the file permissions of your working directory or file, you can change them easily.

How to use chmod to change file permissions

The chmod command is the easiest way to change file permissions in Linux. The general syntax for the command is:

chmod {permission}{operator}{mode}
  • Permission: This represents the permissions given to a user, group, owner, or all.
  • Operator: this indicates permissions given to the permission’s assignee: + grants permission, - denies permissions, and = specifies which permissions to set.
  • Mode: this dictates which permissions to set: r read, w write, or x execute.

For example, to deny everyone write access to the backup directory in the “/var” directory, you can use the command:

 sudo chmod -w /var/backups

Although changing file permissions using the chmod command is effective, it only changes file permissions for the specified directory or file.

Fortunately, you can recursively change the file permissions of a directory or file and its sub-directories and files. To do that, use the chmod command recursive -r option.

For example, let’s recursively remove read permissions for the “/var/backup” directory and all its files and subdirectories. The current permissions are:

drwxrwxrwx  2 root root   4096 Jul 15 06:25 backups

Now let’s run chmod recursively to change the permissions:

sudo chmod -r -r /var/backups

Now you can see that the file permissions have changed.

Chmod Remove Permission 1

Recursive change permission with find

Typically, you will not be setting files and directories with similar permissions. This is because you need the execute permission in order to cd into a specific directory. However, most files do not require this execute permission.

To overcome this, you can use the find utility in Linux. This allows you to only find specific files or directories and set permissions on them.

Let us take the “/var/log” directory. The directory contains both files and directories.

Var Log Files Directories

To set specific permissions for files only, we can use the find command with the -type f option.

See an example command below:

sudo find /var/log/ -type f -exec chmod 777 {} ;

In the above example, we used the find command to search the “/var/log” directory for files, then set read, write and execute permissions for users, groups and all.

We can also do the same case for directories. In this case, we specify the -type d to only get directories.

For example:

sudo find /var/log/ -type d -exec chmod 755 {} ;

In the above example, we only set the permission 755 to directories and not both files and directories.

You can verify this by using the ls -la command.

Chmod With Find Verify

You will notice that all files have the permission 777, while directories have 755 permissions.

In Closing

Knowing how to change file permissions in Linux is an essential skill, as it helps prevent unauthorized access and modifications to various files and directories. Meanwhile, you can also change file ownership to restrict file access or use am access control list for even finer control of file permissions in Linux.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

John Wachira

John is a technical writer at MTE, when is not busy writing tech tutorials, he is staring at the screen trying to debug code.