Previous post:

Next post:

UNIX / Linux chmod Command Examples

by SathiyaMoorthy

What is chmod?
3 chmod examples
Syntax and Options
Related Commands

What is chmod?

chmod stands for change mode, which changes the file or directory mode bits. To put it simply, use chmod command to change the file or directory permissions.

Following is a sample of ls -l command output. In this, the 9 characters from 2nd to 10th position represents the permissions for the 3 types of users.

-rw-r--r--  1 john john  272 Mar 17 08:22 test.txt

In the above example:

  • User (john) has read and write permission
  • Group has read permission
  • Others have read permission

Three file permissions:

  • read: permitted to read the contents of file.
  • write: permitted to write to the file.
  • execute: permitted to execute the file as a program/script.

Three directory permissions:

  • read: permitted to read the contents of directory ( view files and sub-directories in that directory ).
  • write: permitted to write in to the directory. ( create files and sub-directories in that directory )
  • execute: permitted to enter into that directory.

Numeric values for the read, write and execute permissions:

  • read 4
  • write 2
  • execute 1

To have combination of permissions, add required numbers. For example, for read and write permission, it is 4+2 = 6.

3 chmod Examples

Give read, write and execute to everybody (user, group, and others)

read, write and execute = 4 + 2 + 1 = 7.

$ chmod 777 file.txt
(or)
$ chmod ugo+rwx file.txt

Give execute privilege to user. Leave other privileges untouched

execute = 1. If you want to just add execute privilege to users and leave all other privileges as it is, do the following.

$ chmod u+x file.txt

Give read, write and execute privileges to group (including all the files in the sub-directories)

Use -R, as shown below to provide the recursive privileges for the directory and sub-directories (including the files in it).

$ chmod -R g+rwx /u01

Syntax and Options

chmod [OPTION]… MODE[,MODE]… FILE…
chmod [OPTION]… OCTAL-MODE FILE…
chmod [OPTION]… –reference=RFILE FILE…

Short Option Long Option Option Description
-c –changes like verbose but report only when a change is made
–no-preserve-root do not treat `/’ specially (the default)
–preserve-root fail to operate recursively on `/’
-f –silent, –quiet suppress most error messages
-v –verbose output a diagnostic for every file processed
–reference=RFILE use RFILE’s mode instead of MODE values
-R –recursive change files and directories recursively
–help display this help and exit
–versio output version information and exit


Related Commands

chown
umask

Vim 101 Hacks Book

Previous post:

Next post:

Leave a Comment

Previous post:

Next post: