Linux and Unix operating systems provide the chmod
command in order to change access permission for the files and folders. The chmod command name comes from change mode
. The read, write, execute permissions with the sticky bit feature can be changed by using the chmod command. The chmod command is created in 1971 with the Unix operating system which makes it one of the oldest commands used today in a popular way.
chmod Command Syntax
The chmod command is used with options and files and folders. It has the following syntax.
chmod OPTION FILE_FOLDER
- OPTION is chmod command option or permission values about the FILE_FOLDER.
- FILE_FOLDER is the file or folder we want to change its permission.
Display chmod Command Help
Permission Calculation
Before starting to use the chmod command we should learn how the permissions are set and calculated.
Meaning | Letter Expression | Letter Valuation |
---|---|---|
Read Permission | r | 4 |
Write Permission | w | 2 |
Execute Permission | e | 1 |
No Permission | 0 |
The chmod command can be used with both letter permissions or value permissions. For example, we can specify the read and write permission with the w
and r
letters. We can also use the digit presentation by summing the read and write values 4+2 = 6. We can use digit 6 to express read and write permission.
List File and Folder Permissions
Before starting to work with file and folder permissions we generally require to list current permissions about files and folders. The ls
command can be used to list user, group, and other permissions with the current user and group information. The -l
option is added to the ls command.
$ ls -l
Change File and Folder Permission
File permission for the user of the specified file can be changed like below. The user is expressed with the u
letter and the write permission is depicted with the w
. In the following example, we add the write permission for the current user.
$ chmod u+w main.c
The +
sign is used to add permission. If we need to remove already existing permission we can use the -
sign. In the following example, we remove the write permission for the current user.
$ chmod u-w main.c
Alternatively, we can use the absolute or full path of the file or folder in order to change permissions.
$ chmod u-w /home/ismail/main.c
Change Folder Permission Recursively
When we change a folder or directory permission it only affects the specified folder or directory. This does not affect child files and folders in a recursive way. In order to change the permission of all specified folder child files and folders the recursive parameter -r
can be used.
$ chmod -R u+x nmap
Change Group Permission
Generally, user permissions are changed by using the chmod command. But also the group permissions can be changed by using the chmod command. In the following example, we add write
permission for the current group of the file.
$ chmod -R g+w nmap
Change All (User, Group, Other) Permission
Another practical way to change all permissions which is user, group, and other is using the all
like below. In the following example, we set execute permission for all user, group and other.
$ chmod a+x nmap
Display Verbose Output
By default, the chmod command does not provide verbose information about the operations. We can display verbose output by using the -v
option like below.
$ chmod -v a+x nmap
The output is like below which provides information about the permission change.
mode of 'nmap' retained as 0331 (-wx-wx--x)
Quite Mode – Suppress Most of Error Messages
Changing permissions of the files and folders may create different errors related to the permissions etc. These errors are directly printed to the standard output which is generally the command line interface. We can omit and suppress these error messages by using the -f
or --silent
or --quiet
option.
$ chmod -f a+x nmap
or
$ chmod --silent a+x nmap
or
$ chmod --quiet a+x nmap