chmod Recursive: Change File & Directory Permissions Recursively

July 16, 2024

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.

How to change file and directory permissions recursively with chmod - a tutorial.

Prerequisites

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 instructs chmod 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 NotationOctal NotationDescription
uN/AUser (owner) permissions.
gN/AGroup permissions.
oN/AOthers (world) permissions.
aN/AAll users (user, group, and others).
+N/AAdds the specified permissions.
-N/ARemoves the specified permissions.
=N/ASets the specified permissions, removing any others.
r4Read permission.
w2Write permission.
x1Execute permission.
s4 (setuid)Set user or group ID on execution (used with u or g).
t1 (sticky bit)Restricts file deletion in the directory (only the owner can delete).
u+xN/AAdds execute permission for the user (owner).
g-rN/ARemoves read permission for the group.
o=rwN/ASets read and write permissions for others (world), removing any execute permissions.
rwx7Read, write, and execute permissions (4+2+1).
rw-6Read and write permissions (4+2).
r-x5Read and execute permissions (4+1).
r--4Read permission only.
-wx3Write and execute permissions (2+1).
-w-2Write permission only.
--x1Execute permission only.
---0No 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–-):

Check file permission of all files in directory.

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
Change file permission recursively on Linux.

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 the chmod 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:

Setting different permissions for files and directories.

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 from find and applies chmod 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) the chmod 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.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
19 Crucial Linux ls Commands to Know
July 30, 2020

The ls command (short for “list”) lists information about directories and any type of files in the working directory. Learn how to use ls command in this article.
Read more
How to Show Hidden Files in Linux
June 12, 2020

Linux stores visible and hidden files in its directories. You can display these hidden files using simple commands.
Read more
How to Remove (Delete) a File or Directory in Linux
July 16, 2024

This article lists the most commonly used commands and tools to remove unwanted files and directories from Linux.
Read more
How to Use mkdir Command to Make or Create a Linux Directory
December 1, 2023

The mkdir command in Linux allows users to create or make new directories. mkdir stands for "make directory."
Read more