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.
The output above contains specific information.
- The first column represents the file and directory permissions. The first letter in this column indicates the type:
lstands for a symbolic link, and
-a regular file.
- The other nine characters are grouped into three sets:
oowner indicate the file or directory permissions.
rindicates “read” permissions,
windicates “write” permissions, and
Xindicates “execute” permissions.
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
chmod command is the easiest way to change file permissions in Linux. The general syntax for the command is:
- Permission: This represents the permissions given to a user, group, owner, or all.
- Operator: this indicates permissions given to the permission’s assignee:
-denies permissions, and
=specifies which permissions to set.
- Mode: this dictates which permissions to set:
For example, to deny everyone write access to the backup directory in the “/var” directory, you can use the command:
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
For example, let’s recursively remove
read permissions for the “/var/backup” directory and all its files and subdirectories. The current permissions are:
Now let’s run
chmod recursively to change the permissions:
Now you can see that the file permissions have changed.
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.
To set specific permissions for files only, we can use the find command with the
-type f option.
See an example command below:
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.
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.
You will notice that all files have the permission 777, while directories have 755 permissions.
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.