Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Chmod command in Linux with examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In Unix operating systems, the chmod command is used to change the access mode of a file. The name is an abbreviation of change mode. Which states that every file and directory has a set of permissions that control the permissions like who can read, write or execute the file. In this the permissions have three categories: read, write, and execute simultaneously represented by `r`, `w` and `x`. These letters combine together to form a specific permission for a group of users.

The `chmod` command is used to modify this permission so that it can grant or restrict access to directories and files. Let’s have a look at the syntax and options for the `chmod` command in Linux Operating System.

Syntax:

chmod [options] [mode] [File_name] 

“chmod” in Linux [options]

Options Description
`-R` Apply the permission change recursively to all the files and directories within the specified directory.
`-v` It will display a message for each file that is processed. while indicating the permission change that was made.
`-c` It works same as `-v` but in this case it only displays messages for files whose permission is changed.
`-f` It helps in avoiding display of error messages.
`-h` Change the permissions of symbolic links instead of the files they point to.

Note: Options in `chmod` are basically used for making changes in bulk and modifying permissions across multiple files or directories at once.

“chmod” in Linux [mode]

The “mode” helps in setting new permissions that have to be applied to files or directories.

This mode can be specified in several ways, we will discuss two modes: Symbolic and Octal mode. 

1) Symbolic mode

If we talk about symbolic mode, we can say that it is the most common method used for specifying fir permissions. In this we have to make a combination of letters and operators to set or tell what to do with permissions.

The following operators can be used with the symbolic mode:

Operators Definition
`+` Add permissions
`-` Remove permissions
`=` Set the permissions to the specified values

The following letters that can be used in symbolic mode:

Letters Definition
`r` Read permission
`w` Write permission
`x` Execute permission

The following Reference that are used:

Reference Class
u Owner
g Group
o Others
a All (owner,groups,others)

Examples of Using the Symbolic mode:

  • Read, write and execute permissions to the file owner:
chmod u+rwx [file_name]
  • Remove write permission for the group and others:
chmod go-w [file_name]
  • Read and write for Owner, and Read-only for the group and other:
chmod u+rw,go+r [file_name]

2) Octal mode

It is also a method for specifying permissions. In this method we specify permission using three-digit number. Where..

  •  First digit specify the permission for Owner.
  •  Second digit specify the permission for Group. 
  • Third digit specify the permission for Others. The digits 

NOTE: The digits are calculated by adding the values of the individual permissions.

Value Permission
4 Read Permission
2 Write Permission
1 Execute Permission

Examples of Using the Octal mode:

Suppose if we to give read and write permission to the file Owner. Read, write and executable permission to the Group. Read-only permission to the Other. They our command would be.

  chmod 674 [file_name]

Here.

  • 6 represent permission of file Owner which are (rw).
  • 7 represent permission of Group which are (rwx).
  • 4 represent permission of Other which is (r).

Frequently Asked Questions in “chmod” in Linux.

1) How do I view the current permissions of a file or directory?

ls -l
ls -l

all the permissions that a current directory have

2)To see all the permissions that a particular directory or file has.

ls -l example
listed all permissions this file has.

listed all permissions this file has.

Here example is a file_name.

3)What are the different types of permissions in Linux, and what do they mean?

There are three types of permissions in Linux: 

read (`r`)

write (`w`)

execute (`x`)

They are applied to:

owner (`u`)

group (`g`)

other (`o`)

4) What is “chmod 777 “, “chmod 755” and “chmod +x “or “chmod a+x”?

Chmod 777 [file_name]

This command gives all three permissions to everyone (owner, group and other)

chmod a+x [file_name]

It makes a file executable for everyone. It is the most used command after we install an executable file, we still need to add a permission to actually make it an executable file.

chmod 755 [file_name]

In this case the owner can write, read and execute a file, group and other can only read and execute a file.

5) How can we revert changes made by “chmod” command in Linux?

To undo or revert changes made by “chmod” command in Linux , we can use the `chmod` command again but this time we should mention the correct permission we want. 

 Here are the steps to undo or revert changes:

  • Determine the correct permission you want and use `chmod` command again. 
    For Example: If we want to revert the changes to “rw-r–r–” (read and write permission for owner, read-only permission for group and others), according to this our octal value would be “644” (read = 4, write=2).
  • Now open the directory and write the given command :
chmod 644 [file_or_directory_name]

Here instead of “[file_or_directory_name]” use your file or directory name.

Suppose our file name is “a.txt”

Before reverting or undo changes:

ls -l a.txt

ls -l a.txt   (used to display all the permission a.txt has)

After reverting or undo changes:

reverting of chmod changes in Linux

Here we can see that changes has be done

Conclusion

The `chmod` command in Linux is used to modify the permissions and access mode of files and directories. These are the permissions that control who can read, write and execute the file. We have discussed two types of modes for specifying permission: symbolic and octal mode. In symbolic mode, one uses letters and operators to specify the permission. Whereas octal has a three-digit number for specifying the permission. The `chmod` command also provides some options for bulk modifications, for example: `-R` for recursive and `-v` and `-c` for displaying message. The overall conclusion is that `chmod` command in Linux is a very essential tool for managing file and directories permissions.

?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L 


My Personal Notes arrow_drop_up
Last Updated : 26 Apr, 2023
Like Article
Save Article
Similar Reads