Linux chmod Command
Linux chmod command is used to change the access permissions of files and directories. It stands for change mode. It can not change the permission of symbolic links. Even, it ignores the symbolic links come across recursive directory traversal.
In the Linux file system, each file is associated with a particular owner and have permission access for different users. The user classes may be:
The file permissions in Linux are the following three types:
Let's see how to change the file permission using the chmod command.
The basic syntax of chmod command is as follows:
The chmod command supports the following command-line options:
-c, --changes: It is similar to the verbose option, but the difference is that it is reported if a change has been made.
-f, --silent, --quiet: It is used to suppress the error messages.
-v, --verbose: It is used to display a diagnostic for every processed file.
--no-preserve-root: It is used for not treating the backslash symbol ('/'), especially (the default).
--preserve-root: If this option is used, it will fail to operate recursively on backslash ('/').
--reference=RFILE: It is used to specify the RFILE's mode alternatively MODE values.
-R, --recursive: It is used to change files and directories recursively.
--help: It is used to display the help manual having a brief description of usage and support options.
--version: It is used to display the version information.
File Permission Syntax
If you are a new user, you may get confused with the different types of letters used to set the file permission. So, Before proceeding further with the chmod command, let's understand the file permission syntax.
To set the permission of a file or directory, we have to specify the following things:
The permission statement is represented in indicators such as u+x, u-x. Where 'u' stands for 'user,' '+' stands for add, '-' stands for remove, 'x' stands for executable (which).
The user value can be:
u: the owner of the file
g: group member
The permission types can be r, w, and x.
Setting and Updating the Permissions
To set the permission of a file, execute a permission statement with the chmod command. For example, we want to set the read and write permission for all users and groups of file 'Demo.txt.' We have to pass the "u=rw,go=rw Demo.txt" permission statement with chmod command. To display the file permission, execute the below command:
The above command will display the file's current file permission of the 'Demo.txt' file.
To change the permission, execute the below command:
Consider the below output:
From the above output, the access permission of 'Demo.txt' has changed.
Setting Permissions for Multiple Files
We can set permission for multiple files at once by using the chmod command. To change the file permission of multiple files, specify the file pattern with the chmod command. For example, if we want to set read and write permission for all text files, specify the *. txt pattern with chmod command.
To view the permission of all text file from the current working directory, execute the below command:
It will list all the text files with their permission mode. Consider the below output:
From the above output, many files have only read permission for other users.
To set the read and write permission for other users, execute the below command:
It will set the read and write permission for other users of the text files. Consider the below output:
We can use the numeric values instead of letters to specify the permissions. A three-digit value is used to specify the permission. The leftmost digit represents the owner (u), and the middle digit represents the group members (g). The rightmost digit represents the others (o).
The following table represents the digits and their permissions: