Updated April 14, 2023
Introduction to Linux chmod recursive
The Linux operating system is a multi-user system. There is a possibility to access the files and directories via multiple or different users. Hence it is very important to manage the proper access to the data. The chmod command is used to change the permission on the file and directory. But as we said, the Linux environment is multiple users, and we will have the “n” number of files in multiple locations. So the simple chmod command is only used to change the permission of a single file or directory. If we need to change the permission of multiple files and directories at the same time, then recursive comes into the picture. In this topic, we are going to learn about the Linux chmod recursive.
The chmod recursive utility was written by David MacKenzie and Jim Meyering.
Syntax of Text Editors
chmod [ OPTION ]... MODE [ MODE ]... FILE...
chmod [ OPTION ]... OCTAL-MODE FILE...
chmod [ OPTION ]... --reference=RFILE FILE...
- chmod: We can use the “chmod” keyword in the syntax or command. It will take different arguments like OPTION, different types of MODE, reference, or path of the files or directories. As per the provided arguments, it will change the reference file or directory permissions.
- OPTION: We can provide the different flags as the option that is compatible with the “chmod” command.
- MODE: We can provide the different MODE like 4, 2, 1 or r, w, x to the “chmod” command.
- FILE: We can provide the path of the file or directory to the chmod command. We can also provide the file reference as well.
How Linux Chmod Recursive Command Works?
By default, when any user will create the file or directory. The same user having the read and write permission on the newly created file. On the directory level, it will have the read, write, and executable permissions. The chmod command will accept different arguments like options, mode, and the file or directory path. As per the input arguments, the chmod command will change the file or directory permission normally or the recursively (as per the requirement).
Below is the list of options we can use with the chmod or chmod recursive option.
Sr No | Option | Description |
1 | -c, –changes | This option is like the verbose output, but when it finds or report any change is made |
2 | -R, –recursive | It will help to change the files and directories recursively |
3 | -f, –silent, –quiet | It will help to suppress most error messages |
4 | -v, –verbose | It will help to print the output a diagnostic for every file processed |
5 | –no-preserve-root | It will not treat ‘/’ specially (the default) |
6 | –preserve-root | It will fail to operate recursively on ‘/’ location |
7 | –reference=RFILE | It will help to use the RFILE’s mode instead of MODE values |
8 | –help | It will print the help options of the chmod command and exit |
9 | –version | It will print the version information of the chmod command and exit |
Examples of implementing Linux Chmod Recursive Command
Given below are the examples of Linux chmod recursive:
Example #1 – Change File Permission
Changing the recursive file permission is a very common and simple task in the Linux operating system. But, first, we need to use the “-R” option with the chmod command.
Command :
chmod -R 777 data
Explanation :
As per the above command, we are changing the file permission. Currently, the file permission is rw, r, r, and we are changing it to rwx, rwx, rwx recursively (refer to screenshot 1 (a) and screenshot 1 (b)).
Output :
Screenshot 1 (a)
Screenshot 1 (b)
Example #2 – Change Directory Permission
It is a very common way to change the recursive directory permission in the Linux operating system. We need to use the “-R” option with the chmod command.
Command :
chmod -R 777 data
Explanation :
As per the above command, we are changing the directory permission. Currently, the file permission is rwx, rx, rx, and we are changing it to rwx, rwx, rwx recursively (refer to screenshot 2 (a), and screenshot 2 (b)).
Output :
Screenshot 2 (a)
Screenshot 2 (b)
Example #3 – With Symbolic Method
In the chmod command, we are having the functionality to change the permission of files or directories recursively via the symbolic method.
Command :
chmod -R u=rwx,go=rwx data
Explanation :
As per the above command, we are changing the recursive file and directory permission. In screenshot 3(a), we are having the default permission. With the help symbolic method, we are changing the file and directory permission recursively (refer to screenshot 3(b)).
Output :
Screenshot 3 (a)
Screenshot 3 (b)
Example #4 – With Find Command
As per the requirement, we can disconnect or down the network interface from the Linux environment. Then, we just need to define the interface name in the nmcli command.
Command :
find . -type d -exec chmod -R 777 {} \;
Explanation :
With the help of the find command, we are changing the file and directory permission recursively. First, in the find command, we are looking at the current working directory (refer to screenshot 4 (a)), i.e. “/root/data,” then we are changing the file and directory permission “777” recursively (refer screenshot 4 (b)).
Output :
Screenshot 4 (a)
Screenshot 4 (b)
Example #5 – With Input Arguments
In the chmod command, we are having the functionality to get input arguments, and accordingly, we can change the file or directory permission recursively.
Command :
find . -type d -print0 | xargs -0 chmod -R 777
Explanation :
As per the above command, we use different commands like find, chmod, xargs. With the help of the find command, we find the files and directories in the current working directory, i.e. “/root/data”. With the help of pipe “|” and xargs command, we are providing the input to the chmod command (changing the permission recursively).
Output :
Screenshot 5 (a)
Screenshot 5 (b)
Conclusion
We have seen the uncut concept of “Linux Chmod Recursive” with the proper example, explanation, and command with different outputs. First, we need to manage the proper access or permission on the files or directories in the Linux environment. Then, we can use the chmod command with different commands like find, xargs, etc.
Recommended Articles
We hope that this EDUCBA information on “Linux chmod recursive” was beneficial to you. You can view EDUCBA’s recommended articles for more information.