Introduction
Multi-user systems, such as Linux, require setting up and managing file permissions that ensure only authorized users have access to the relevant files. The chmod
command allows you to change a single file's permission or change permissions recursively to configure multiple files and subdirectories with a single command.
In this tutorial, you will learn how to use chmod recursively and change file permissions in Linux.
Prerequisites
- A machine running Linux.
- A user account with root privileges.
chmod Recursive Syntax
The chmod
command allows users to change the permissions of files and directories in a Linux system. To recursively change the permissions on all files and directories in a specified directory, use the -R
(--recursive
) option.
The syntax for recursively changing permissions is:
chmod -R [permissions] [directory]
-R
instructschmod
to apply the permission change recursively to all files and subdirectories within the specified directory.[permissions]
specifies the new permissions you want to set. The command accepts symbolic or numeric permissions (e.g.,755
,644
,u+rwx
, etc.).[directory]
is the directory where you want to apply the changes.
Note: The user who creates a file (or directory) has ownership of it. The owner has read, write, and execute privileges. Other users only have as much access as given to them when configuring permissions. However, the root user has all privileges for all files.
chmod -R Options
The chmod
-R
option makes the command recursive, meaning it applies the specified permissions to the directory and all its contents, including subdirectories and files.
The following table explains some of the [permissions]
you can set with the chmod -R
command, in their octal and symbolic form:
Symbolic Notation | Octal Notation | Description |
---|---|---|
u | N/A | User (owner) permissions. |
g | N/A | Group permissions. |
o | N/A | Others (world) permissions. |
a | N/A | All users (user, group, and others). |
+ | N/A | Adds the specified permissions. |
- | N/A | Removes the specified permissions. |
= | N/A | Sets the specified permissions, removing any others. |
r | 4 | Read permission. |
w | 2 | Write permission. |
x | 1 | Execute permission. |
s | 4 (setuid) | Set user or group ID on execution (used with u or g ). |
t | 1 (sticky bit) | Restricts file deletion in the directory (only the owner can delete). |
u+x | N/A | Adds execute permission for the user (owner). |
g-r | N/A | Removes read permission for the group. |
o=rw | N/A | Sets read and write permissions for others (world), removing any execute permissions. |
rwx | 7 | Read, write, and execute permissions (4+2+1). |
rw- | 6 | Read and write permissions (4+2). |
r-x | 5 | Read and execute permissions (4+1). |
r-- | 4 | Read permission only. |
-wx | 3 | Write and execute permissions (2+1). |
-w- | 2 | Write permission only. |
--x | 1 | Execute permission only. |
--- | 0 | No permissions. |
You can combine some of the notations above to set the permissions according to your needs. For example, u+x
adds the execute permission for the owner while u-x
removes the execute permission from the owner.
Similarly, o-rw
removes the read and write permission from everyone except the owner and group owner. On the other hand, u+x,go=rx
adds the execute permission for the owner and sets the permissions for the group and others to read and execute.
For more hands-on examples, refer to the sections below.
chmod Recursive Examples
This section shows examples of using chmod
and different ways to set permissions for files or directories.
Check Permissions
Before changing a file or directory's permissions, it is recommended to check the current permissions. To check the file permissions in the working directory, use the command below:
ls -l
The output lists the permissions of all the files and directories in the specified path.
For instance, the Example directory contains three files (test1.txt, test2.txt, and test3.txt) with the same permissions (-rw-rw-r–-
):
The file permissions listed above tell us the following:
- The owner has read and write privileges.
- The owner's group has read and write privileges.
- Other users have read privileges.
Note: Do you want to learn more about file permissions and how they are defined? Refer to the Linux File Permissions tutorial.
Change Single File/Directory Permissions
The basic syntax for changing a specific file or directory permissions is:
chmod [permission] [file_name/directory]
Specify the [permissions]
in symbolic or absolute (numeric) mode. Read our in-depth article on file permissions to learn different ways to set permissions.
Change Permissions Recursively Using chmod
The section above showed how to use the chmod
command to change a single file or directory permissions. However, you may need to modify the permissions recursively for all files within a directory.
Specify the recursive option (-R
or --recursive
) with chmod
to recursively change permissions for multiple files. The option sets the permissions for the directory (and the files it contains).
The syntax for changing the file permissions recursively with chmod
is:
chmod -R [permission] [directory]
Therefore, to set the 755
permission for all files in the Example directory, use the command below:
sudo chmod -R 755 Example
The command gives read, write, and execute privileges to the owner (7) and read and execute access to everyone else (55).
Note: In the example above, the permission is defined using the octal/numerical mode (755
). Alternatively, you can utilize the symbolic mode (alphanumerical characters) and use the command:
chmod -R u=rwx,go=rx Example
Change Permissions Recursively Using chmod and find Command
Using chmod
in combination with find allows for more granular and flexible control over changing file and directory permissions. This is particularly useful when you need to apply different permissions to files and directories or when dealing with a large directory tree.
The basic syntax includes using the find
command to locate files/directories and then passing it on to chmod
to set the permissions:
sudo find [directory] -type [d/f] -exec chmod [permissions] {} \;
- Replace
[directory]
with the directory path that holds the files and subdirectories you want to configure. - Specify whether you are searching for a directory
-type d
or a file-type f
. - Set the file
[permissions]
with thechmod
command using the numerical or symbolic mode.
Avoid assigning execute privileges to files. A common setup would include running the following commands:
sudo find Example -type d -exec chmod 755 {} \;
sudo find Example -type f -exec chmod 644 {} \;
In this example, the directories have 755
(u=rwx,go=rx
) permissions, while the files have 644
(u=rw,go=r
) permissions.
You can check to verify directories and files have different permission settings by moving into the Example directory (cd Example
) and listing the content (ls -l
). The output should be similar to the one below:
Note: Learn more about the Linux find command.
Change Permission of Specific File Type
Combine the find
command with chmod
to change the permissions for files of a specific type. The syntax for changing the permissions of a specific file type in a directory is:
find [directory] -name "*.[filename_extension]" -exec chmod [permissions] {} \;
For example, to make all .sh files in the current directory executable, use:
find . -name "*.sh" -exec chmod +x {} \;
find xargs chmod
The find
command, combined with xargs, is a powerful way to apply chmod
recursively with fine control over which files and directories are affected. This method is efficient for handling a large number of files.
The syntax is:
find /path/to/directory -type f -print0 | xargs -0 chmod 644
find /path/to/directory -type d -print0 | xargs -0 chmod 755
- The first command finds all files (
-type f
) within the specified path and prints them in a null-terminated format (-print0
). The format helps handle filenames with spaces. xargs -0 chmod 644
takes the list of files fromfind
and applieschmod 644
to each.- Similarly, the second command applies
chmod 755
to all directories (-type d
).
find -exec chmod
The find
command with the -exec
option provides another method to apply chmod
recursively. This method directly executes the chmod
command on each file and directory found.
The syntax is:
find /path/to/directory -type f -exec chmod 644 {} \;
find /path/to/directory -type d -exec chmod 755 {} \;
- The first command finds all files (
-type f
) within the specified path and executes (-exec
) thechmod 644
command on each file found. {}
represents the current file and\;
indicates the end of the command.- The second command applies
chmod 755
to all directories (-type d
).
Conclusion
You now know how to recursively change the file permission on your Linux system with chmod -R
or the find
command. The recursive permission setting is extremely useful when working with many files or with an extensive directory tree.
Next, learn about the umask command, which lets you change the default permissions.