Saturday, December 28, 2024
Google search engine
HomeSecurity & TestingHow to Change File Permissions Recursively with chmod in Linux

How to Change File Permissions Recursively with chmod in Linux

Introduction

Multi-user systems, such as Linux, require setting up and managing file permissions that ensure only authorized users have access to files they are supposed to.

If you need to change a file permission, use the chmod command. It also allows to change the file permission recursively to configure multiple files and sub-directories using a single command.

In this tutorial, you will learn how to use chmod recursively and change file permission on Linux.

How to recursively change file permissions in Linux.How to recursively change file permissions in Linux.

Prerequisites

  • A command line / terminal window (Ctrl+Alt+T or Ctrl+Alt+F2)
  • A user account with sudo privileges (optional)
  • A Linux system

Note: The user who creates a file (or directory) has ownership of it. The file-owner has read, write, and execute privileges. Other users only have as much access as given to them when configuring permissions, while the root user has all privileges for all files.

Check File Permission

If you need to check the file permissions in the working directory, use the command:

ls –l

The output lists the permissions of all the files in the directory.

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.Check file permission of all files in directory.

The file permissions listed above tells 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 Permission Tutorial.

Change Permission Recursively

It is common to use the basic chmod command to change the permission of a single file. However, you may need to modify the permission recursively for all files within a directory.

In such cases, the chmod recursive option (-R or --recursive) sets the permission for a directory (and the files it contains).

The syntax for changing the file permission recursively is:

chmod -R [permission] [directory]

Therefore, to set the 755 permission for all files in the Example directory, you would type:

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).

Change file permission recursively on Linux.Change file permission recursively on Linux.

Note: In the example above, the permission is defined using the octal/numerical mode (755). Alternatively, you can utilize the symbolic mode (using alphanumerical characters) and use the command: chmod -R u=rwx,go=rx Example.

Change Permission With the find Command

To assign separate permissions to directories and files, you can use the find command.

The basic syntax includes using the find command to locate files/directories and then passing it on to chmod to set the permission:

sudo find [directory] -type [d/f] -exec chmod [privilege] {} \;
  • Replace [directory] with the directory path that holds the files and subdirectories you want to configure.
  • Specify whether it is searching for a directory -type d or a file -type f.
  • Set the file [privilege] 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) privileges, while the files have 644 (u=rw,go=r) privileges.

Change file permission recursively using the find command.Change file permission recursively using the find command.

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.Setting different permissions for files and directories.

Note: Learn more about the Linux find command.

Change Permission of Specific Files Recursively

Combining the find command with chmod can also be used for changing the permission of files that are a specific type.

The command syntax for changing the permission of a specific file type in a directory is:

find [directory] -name "*.[filename_extension]" -exec chmod [privilege] {} \;

For example, to make all .sh files in the current directory executable, you would use:

find . -name "*.sh" -exec chmod +x {} \;

Conclusion

You should now know how to recursively change the file permission on your Linux system with chmod -R or the find command.

Alternatively. you can also use the umask command which lets you change the default permissions.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments