Managing file permissions in Linux is an important aspect of system administration. In this multi-user environment, it’s vital to restrict or grant access to users based on their roles & responsibilities. Hence, by setting appropriate permissions, you can restrict or allow access to files and directories for different users on the system. In this article, I will discuss different ways how you can change Linux folder permissions recursively with some practical examples.
Key Takeaways
- Learning to change folder permissions recursively using both CLI & GUI.
- Get to learn how to change directory ownership recursively.
Requirements
- To change or modify file or folder permissions in Ubuntu, you must either be the root user or have root/sudo privileges.
- Moreover, you need the necessary permissions to access a directory along with its contained files & sub-folders in order to change it recursively.
[Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS]
Watch 2 Methods to Change Linux Folder Permissions Recursively
2 Methods to Change Linux Folder Permissions Recursively
In Linux, changing file & folder permissions recursively means recursive operations are applied to all files & subdirectories within a particular directory. Therefore, let’s discuss different ways to change folder permissions recursively. I will show the process using both CLI & GUI.
Method 01: Change Permissions Recursively Using the Terminal in Linux
In Linux, you can easily & quickly change any folder permission recursively using the Command Line Interface (CLI). In this case, the command that is used to change permissions is the chmod command.
Moreover, you can use some other commands like the find command along with the chmod command to modify the permission settings recursively. Read the following examples for detailed information on the cases.
Example 01: Use the Chmod Command to Change Linux Folder Permissions Recursively
To recursively change the Linux folder permissions, you can use the chmod command with the –recursive or -R option. Hence use the command either in the absolute mode of the chmod command or the symbolic mode of the chmod command. In order to change directory permissions recursively follow the below steps:
Steps to Follow >
➊ At first, open your Ubuntu terminal application.
➋ Then, write the following command in your command prompt to view home directory contents,
ls -l
- ls command: Lists directory contents.
- -l –long: Option of the ls command where -l gives long lists.
➌ Press the ENTER button.
➍ Next, change the ‘Documents’ directory permission recursively using the following command:
Command Syntax (symbolic) >
chmod -R permission_group_name+/-permission_type /location/path/of/file
chmod -R a+rwx Documents
OR,
Command Syntax (absolute) >
chmod -R absolute value /location/path/of/file
chmod -R 777 Documents
- chmod: Changes access permission of files & directories.
- -R: Option of the chmod command (Recursive).
- 777: Absolute value of the chmod command where, 7 denotes read-write-execute for all (Owner, Group & Others).
- Documents: One of the Directory.
➎ Subsequently, hit the ENTER key & check the changed directory permission attributes using the ls command:
ls -l | grep Documents
- grep: Looks for a specific pattern given to it as an argument.
- Documents: One of the system folders.
At first, you can see from the output of the ls command is showing the permission attributes (drwxr–r–) for the directory “Documents”. Afterward, run the command & it sets specified permissions (777) for the directory recursively. At this point, check the result by running the ls -l command again. The command shows permission attributes as (drwxrwxrwx).
➏ Afterward, type the following command to change the working directory to the ‘Documents’ directory to view permission changes to all files & sub-directories inside the directory.
cd Documents
- cd command: Changes working directory.
- Documents: One of the Directory.
➐ Press ENTER.
➑ Subsequently, check the directory contents to check if the command has changed permissions recursively using the list command:
ls -l
➒ Finally, press the ENTER button again. Notice from the output, each & every file & sub-directory within the “Documents” directory has the same permission settings. The chmod command has changed the permission as specified, recursively.
Example 02: Change Folder Permissions Recursively Using the find command in Linux
Another way to change folder permissions recursively is that you can use the find command. It looks for all files or sub-directories inside a directory & then passes those to the chmod command to execute individually using the exec command to change their permissions. Go through the below steps, to see how it works.
Steps to Follow >
➊ First, open your terminal.
➋ Then, type the following command in your command prompt:
Command Syntax >
find /location/path/to/folder options -exec chmod permission_values {} \;
find /home/munny/Downloads -type d -exec chmod 750 {} \;
- find: Searches for files & directories in a specified location based on provided options.
- home/munny/Downloads: Location path to the “Downloads”
- -type d: find command option to search for specific (-d) ‘directory’
- -exec: Used to execute the chmod command on each file found by the find command.
- chmod: Changes access permission of files & directories.
- 750: Absolute value of the chmod command where ‘7’ denotes read-write-execute for the Owner, ‘5’ denotes read-execute permission for the Group & ‘0’ represents ‘no permission’ for Others.
- {} : A placeholder that represents the name of the found file & is replaced with the actual file name when the command is executed.
- \; : indicates the end of the command.
➌ Press the ENTER key.
➍ Afterward, type the following command to change the working directory to the ‘Downloads’ directory to view permission changes to all files & sub-directories inside the directory.
cd Downloads
➎ Press the ENTER key.
➏ At this point, check the contents using the list command, and finally, press the ENTER button again:
ls -l
The find command upon execution, searches for all ‘directory’ types that reside in the “Download” folder & then passes the result to the chmod command to execute. Subsequently, the chmod command changes specified permissions (750) for the directory recursively. At this time, you can see the result by running the ls -l command, showing permission attributes as (drwxr-xx–) for the sub-directory “ella doo”.
Example 03: Change Permissions Recursively for Specific File Types within a Folder in Linux
Let’s say you want to change permissions for all text files inside a directory but not other file types, in that case, you can use the find command with the -name option & then give the file name or extension inside the double quotation (“ ”) to search for all same type files. Subsequently, pipe those files as a bundle of standard inputs to the chmod command using the xargs command.
Follow the below steps with command syntax:
Steps to Follow >
➊ First, open your terminal.
➋ Next, type the following command in your command prompt:
Command Syntax >
find /location/path/to/folder options | xargs chmod permission_values
find /home/munny/Downloads -type f -name “*.txt*” | xargs chmod 700 {} \;
- find: Searches for files & directories in a specified location based on provided options.
- home/munny/Documents: Location path to the “Documents”
- -type f: find command option to search for specific (-f) ‘file’
- -name: Option of the find command to search for files with a specific name or pattern.
- “*.txt*”: To search for all files with “.txt”
- Pipe (|): Redirects the standard output.
- xargs: Builds up inputs into a bundle that is provided as an argument list to the next command.
- chmod: Changes access permission of files & directories.
- 700: Absolute value of the chmod command where ‘7’ denotes read-write-execute for the Owner, & ‘0’ represents ‘no permission’ for Groups &
➌ Press the ENTER key.
➍ Afterward, type the following command to change the working directory to the ‘Downloads’ directory to view permission changes to all files & sub-directories inside the directory.
cd Downloads
➎ Press the ENTER key.
➏ Subsequently, check the contents executing the list command:
ls -l | grep .txt
- ls: Lists directory contents
- -l –long: Option of the ls command where -l gives long lists.
- grep: Looks for a specific pattern given to it as an argument.
- .txt: Files with .txt
➐ Finally, press the ENTER button again.
The find command upon execution, searches for all ‘files’ with the extension “*.txt*” that reside in the “Documents” folder & then pipe the result to the xargs command. The xargs command creates a bundled standard input for the chmod command to set permission on each file found recursively. Hence, the chmod command changes specified permissions (700) for all the text files recursively. Afterward, see the result by running the ls -l command, showing permission attributes as (-rwx——) for all the text files.
- How to Give Permission to User in Linux? [4 Methods]
- 2 Ways to Change Folder Permissions from Root to User in Linux
Example 04: Set 2 Different Permissions for All Files & Sub-Folders Inside a Directory
In this example, let’s try to set two different permissions for all the files & sub-folders inside the directory. This command variation could be surprisingly useful in the case, where you need to set one type of permission for all the files whereas another type of permission for all the sub-folders within a directory. Therefore, go through the below steps to perform practically:
Steps to Follow >
➊ First, open your terminal.
➋ Then, type the following commands in your command prompt:
Command Syntax >
find /location/path/to/folder options -exec chmod permission_values {} \;
find /home/munny/Documents -type d -exec chmod 750 {} \; &&
find /home/munny/Documents -type f -exec chmod 644 {} \;
- find: Searches for files & directories in a specified location based on provided options.
- home/munny/Downloads: Location path to the “Downloads”
- -type d: find command option to search for specific (-d) ‘directory’
- -type f: find command option to search for specific (-f) ‘file’
- -exec: Used to execute the chmod command on each file found by the find command.
- chmod: Changes access permission of files & directories.
- 750: Absolute value of the chmod command where ‘7’ denotes read-write-execute for the Owner, ‘5’ denotes read-execute permission for the group & ‘0’ represents ‘no permission’ for others.
- 644: Absolute value of the chmod command where ‘6’ denotes read-write permission for the Owner, ‘4’ denotes read-only permission for the Group &
- {} : A placeholder that represents the name of the found file & is replaced with the actual file name when the command is executed.
- \; : Indicates the end of the command.
- && : A operator that means that the next command should only be executed if the first command succeeds.
➌ Next, press the ENTER key.
➍ Afterward, type the following command to change the working directory to the ‘Downloads’ directory to view permission changes to all files & sub-directories inside the directory.
cd Downloads
➎ Press the ENTER button.
➏ Subsequently, check the contents executing the list command:
ls -l
➐ Finally, press the ENTER button again.
At first, two separate commands have been run with && separator. And, it has been used for two different permission settings for all the files & sub-directories inside the “Documents” directory. Afterward, you can see the resulting different permissions for the files & directories by changing the working directory to ‘Documents’ & then running the ls -l to view its contents with changed permissions. Therefore, from the output image, see different permissions for files (-rw-r–r–) & sub-directories (drwxr-x—).
Method 02: Change Permissions Recursively Using GUI in Linux
When you are not sure which command to use or getting unexpected errors while using CLI to change folder permission recursively, you can always use the Graphical User Interface (GUI). Even though it could be a little more time-consuming, it is handy for beginners without CLI knowledge.
Steps to Follow >
➊ At first, go to your file manager & from there select the folder you want to change permissions of & right-click on it. A bunch of options will pop up, anyway you will notice the “Properties” option at the bottom, click on it. ➋ Afterward, click on the permissions. From there you will notice 3 permission groups, along with their permission box. And at the bottom, you will see “Change permissions for enclosed files” which is denoting all files & sub-folders inside the directory, click on it.
➌ Subsequently, on the next page you will get the permission-changing options for both the ‘Files’ & ‘Folders’ in two columns inside the directory. From there set the appropriate file permission for Owner, Group & Others.
➍ Next, check out the folder permissions available to set from the GUI, which are listed below.
Permission Options | Access Type |
---|---|
Read-only | Read-only (r) |
Read and write | Read-Write (r-w) |
Access files | Read & Execute (r-x) |
List files only | Read-Only (r) |
Create and delete files | ALL ( r-w-x) |
None | No access |
Then, select the permission as per your need. I have changed Group & Others permissions to ‘none’ for all the sub-folders inside the directory.To sum up, in this way you can change any folder permissions recursively on your system using GUI. Moreover, you do not need any separate task to view updated permissions as it is visible in the GUI.
Comparative Analysis of Methods
In this article, I showed you how to modify files & folder permissions recursively using both GUI & CLI. Anyway, check the following table where I have comparatively analyzed the pros & cons of both methods.
Methods | Pros | Cons |
---|---|---|
Method 1 |
|
|
Method 2 |
|
|
In summary, both methods have their pros and cons. If you have good CLI knowledge and feel more comfortable using it over GUI, use method 1. On the other hand, if you are unsure about some commands or facing unknown errors, use method 2.
Complementary Information
Besides, knowing about folder permissions & how to modify them recursively, you will find the below information helpful.
Change Folder Ownership Recursively in Linux
To change folder or directory ownership recursively in Linux, you can use the chown command with the –recursive or -R option. This will change ownership for the entire folder along with its contents (files or subfolders) inside it. Follow the below steps with the syntax for the practical example:
Steps to Follow >
➊ At first, open your Ubuntu terminal application.
➋ Then, type the following command in your command prompt:
Command Syntax >
sudo chown options user_name /folder/path/location
sudo chown -R tom .home.munny/Music
- sudo: permits administrative privileges.
- chown: Changes ownership of files & directories.
- -R: Option of the chown command (Recursive).
- tom: User name.
- /home/munny/Music: “Music” directory location path.
➌ Next, press the ENTER button.
➍ Subsequently, check directory ownership & permission attributes using the following command:
ls -l | grep Music
- ls: Lists directory contents
- -l –long: Option of the ls command where -l gives long lists.
- grep: Looks for a specific pattern given to it as an argument.
- Music: One of the system folders.
➎ Afterward, type the following command to change the working directory to the ‘Music’ directory to view permission changes to all files & sub-directories inside the directory.
cd Music
- cd: Changes working directory.
- Music: One of the Directory.
➏ Press the ENTER key.
➐ At this point, check the directory contents executing the list command:
ls -l
➑ Finally, press the ENTER button again.
After running the command, it changes the folder ownership recursively. At this point, check the result by running the ls -l command again, which will show permission attributes as (drwxrwxrwx) for the directory along with the new owner ‘tom’ of the directory. Next, cd into the directory to check that all the files inside it also have the new owner “tom”. Hence, the command has changed ownership recursively.
- How to Change File Permissions to 777 in Ubuntu?
- How to Change Folder Permissions in Linux? [2 Methods]
Change Group Permissions Recursively in Linux
To change permissions for a group of users recursively, follow the below command syntax & act according to the below steps.
Steps to Follow >
➊ Start by opening your Ubuntu terminal application.
➋ Next, type the below command to select the folder you want to change permissions recursively for the group. Here, I will change permission for the groups for my “Pictures” folder.
ls -l | grep Pictures
- ls: Lists directory contents
- -l –long: Option of the ls command where -l gives long lists.
- grep: Looks for a specific pattern given to it as an argument.
- Pictures: One of the system folders.
➌ Press the ENTER key.
➍ Then type the following command to change ‘Pictures’ folder permissions recursively for the ‘Group’:
Command Syntax >
<strong>chmod option permission_group+/-permission_types /location/path/to/folder</strong>
chmod -R g+rw Pictures
- chmod: Changes access permission of files & directories.
- -R: Option of the chmod command (Recursive).
- g+rw: Adding read-write (r-w) permissions to Groups (g).
- Pictures: One of the directory names.
➎ Press the ENTER button.
➏ Subsequently, view the changed folder permissions using the list command:
ls -l | grep Pictures
At first, you can see from the output of the ls -l command is showing the permission attributes (drwx——) for the directory “Pictures”. Afterward, run the command & it sets specified permissions (g+r) for all the users of the Group of that directory recursively. Subsequently, see the result by running the ls -l command again, showing permission attributes as (drwxrw—-).
➐ Next, type the following command to change the working directory to the ‘Pictures’ directory to view the changed permission to all files & sub-directories inside the directory.
cd Pictures
➑ Press the ENTER key.
➒ Subsequently, check the contents by executing the list command in the command prompt:
ls -l | grep Pictures
➓ Finally, tap the ENTER key again.At first, change the working directory to the “Pictures” directory with the cd command. Then, view all contents list with their changed (-rw-rw-r–) permission. Thus, you can see the command has changed permissions for all file group permissions recursively.
Conclusion
In conclusion, changing Linux folder permissions recursively is a crucial aspect of managing files & folders. In this article, I have shown how to do that using both the CLI & GUI. Moreover, I used different command combinations to modify the permission settings. Anyway, if you go through the examples I provided, you will get most of the ways to change folder permissions recursively. Happy Learning!
People Also Ask
Related Articles
- How to Change Permissions of All Files in a Folder in Linux?
- Manage File Permissions in Ubuntu Using Command?
- How to Change File Permissions in Linux with 6 Examples
- 2 Cases to Give User Permission to Folder and Subfolders in Ubuntu
- How to Change Directories and Files Recursively with “chmod”
- Change Permissions on Mounted Drive in Linux