Javatpoint Logo
Javatpoint Logo

Chmod Command in Linux/Unix with Examples

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:

  • owner
  • group member
  • Others (Everybody else)

The file permissions in Linux are the following three types:

  • read (r)
  • write (w)
  • execute (x)

Brief History of Chmod

First, the chmod command is represented in AT&T UNIX version 1 with the chmod system call. The access-control lists were included in several file systems in inclusion to these most common modes to enhance flexibility because systems grew in types and a number of users.

The chmod version arranged in GNU coreutils was specified by Jim Meyering and David MacKenzie. This command is present as an isolated package for Microsoft Windows as an element of the UnxUtils native Win32 port collection of basic GNU Unix-like utilities. Also, the chmod command has been shipped to the IBM i OS.

Let's see how to change the file permission using the chmod command.

Syntax:

The basic syntax of chmod command is as follows:

Generally implemented options are:

  • -R: It stands for recursive, i.e., add objects to subdirectories.
  • -V: It stands for verbose, display objects modified (unmodified objects are not displayed).

The target object is influenced if a symbolic link is mentioned. File modes related to symbolic links themselves directly are not used typically.

The primary component of the chmod permission:

For instance, rwxr-x---

All groups of three characters specify permissions for all classes:

  • rwx: The leftmost three characters specify permissions for the file owner (i.e., the User class).
  • r-x: The three middle characters specify permissions for the group owning the file (i.e., the Group class).
  • ---: The three rightmost characters specify permissions for the Other class. Users who aren't the file owner and group members can't access the file.

Options:

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:

  • Who: Who we are. (user)
  • What: What change are we going to made ( Such as adding or removing the permission)?
  • Which: Which of the permissions?

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

o: others

a: all

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:

Linux chmod Command

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:

Linux chmod Command

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:

Linux chmod Command

Numerical Shorthand

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:

Digits Permissions
000 No permission
001 Execute permission
010 Write permission
011 Write and execute permissions
100 Read permission
101 Read and execute permissions
110 Read and write permissions
111 Read, write, and execute permissions

Symbolic modes

Also, the chmod command accepts the finer-grained symbolic notation, which permits changing specific modes. The symbolic mode consists of three elements, which are merged to form a single text string:

The chmod program applies an operator to define how the file modes should be arranged. The below operators are approved:

Operator Description
+ It adds the described to the described classes.
- It removes the described mode from the described classes.
= It represents that the modes described are to be created the same modes for the described classes.

The modes represent which permissions will to be removed or granted from the described classes. There are mainly three common modes that are related to the common permissions:

Name Mode Description
read r It reads a file or lists the contents of a directory.
write w It writes to a directory or file.
execute x It recurses a directory tree or executes a file.
special execute X It is not permission but instead can be used rather than x. It uses the execute permissions for directories despite their current permissions and uses the execute permissions for a file that has at least an execute permissions bit set. It is helpful if used with the "+" operator and without setting the execute permission which would happen if we just used chmod -R a+rx ., whereas we can implement chmod -R a+rx . with x rather.

Multiple modifications can be described by isolating multiple symbolic modes along with commas. The chmod command will inspect the umask if a user isn't specified.

Special modes

Also, the chmod command can change the special modes and extra permissions of a directory or file. The symbolic modes apply 's' to indicate the setgid and setuid modes and 't' to indicate the sticky mode. A mode is only used for the correct classes, despite whether other classes are mentioned or not.

Almost all operating systems numerically support the special mode specification, specifically in octal, but a few don't. Only the symbolic modes can be applied to these systems.

Some examples of the command line:

Command Description
chmod a+r publicComments.txt It will add the read permission for every class (i.e., Group, Owner, and Others).
chmod a-x publicComments.txt It will remove the execute permission for every class.
chmod a+rx viewer.sh It will add the execute and read permissions for every class.
chmod u=rw, g=r, o=internalPlan.txt It will set the write and read permissions for the user, ser read for Group, and reject access for Others.
chmod -R u+w, go-w docs It will include the write permission into the directory docs and each of its content for the owner and deletes the write permission for others and group.
chmod ug=rw groupAgreements.txt It will set the write and read permissions for Group and user.
chmod 664 global.txt It will set the write and read permissions for Group and user and gives the read permission to Others.
chmod 744 Show_myCV.sh It will set the execute, write, and read permissions for the user and gives the read permission to Group and Others.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA