Chmod Command in Linux/Unix With Examples | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

chmod = Change mod

chmod command is used to change the access mode of a file and directory.

~/codeFactory$ chmod --help 
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
  or:  chmod [OPTION]... OCTAL-MODE FILE...
  or:  chmod [OPTION]... --reference=RFILE FILE...
Change the mode of each FILE to MODE.
With --reference, change the mode of each FILE to that of RFILE.

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

Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report chmod translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/chmod>
or available locally via: info '(coreutils) chmod invocation'

The references are used to distinguish the users to whom the permissions apply i.e. they are list of letters that specifies whom to give permissions. The references are represented by one or more of the following letters:

Reference   Class     Description
u          owner      file's owner

g          group      users who are members of
                      the file's group

o          others     users who are neither the
                      file's owner nor members of 
                      the file's group

a          all       All three of the above, same as ugo

The operator is used to specify how the modes of a file should be adjusted. The following operators are accepted:

Operator  Description
+         Adds the specified modes to the
          specified classes

-         Removes the specified modes from 
          the specified classes

=         The modes specified are to be made
          the exact modes for the specified 
          classes

We can use 0 to 7 to change permission:

0 means False/Revoke and 1 means True/Grant

    RWX
0 - 000
1 - 001 (Execute permission)
2 - 010 (Write permission)
3 - 011 (Write, Execute permission)
4 - 100 (Read permission)
5 - 101 (Read, Execute permission)
6 - 110 (Read, Write permission)
7 - 111 (Read, Write, Execute permission)

The modes indicate which permissions are to be granted or removed from the specified classes. There are three basic modes which correspond to the basic permissions:

r       Permission to read the file.
w       Permission to write (or delete) the file.
x       Permission to execute the file, or, in
        the case of a directory, search it.

How to change directory permissions in Linux

To change directory permissions in Linux, use the following:

  • chmod +rwx filename to add permissions.
  • chmod -rwx directoryname to remove permissions.
  • chmod +x filename to allow executable permissions.
  • chmod -wx filename to take out write and executable permissions.

This only changes the permissions for the owner of the file.

~/codeFactory$ ll
total 8
drwxr-xr-x 2 user user   2 Aug 16 16:29  Download/
drwxr-xr-x 2 user user   2 Aug 16 16:29  Photos/
drwxr-xr-x 2 user user   2 Aug 16 16:53 'Photos and Videos'/
drwxr-xr-x 2 user user   2 Aug 16 16:29  Videos/
---------- 1 user user 149 Aug 12 15:32  test.txt
-rw-r--r-- 1 user user  26 Aug 12 16:29  test2.txt
-rw-r--r-- 1 user user  37 Aug 14 06:20  test3.txt

How to Change Directory Permissions in Linux for the Group Owners and Others

To change directory permissions for everyone, use “u” for users, “g” for group, “o” for others, and “ugo” or “a” (for all).

~/codeFactory$ ll
total 8
drwxr-xr-x 2 user user   2 Aug 16 16:29  Download/
---------- 1 user user 149 Aug 12 15:32  test.txt

~/codeFactory$ chmod g-rx Download/

~/codeFactory$ chmod o+w test.txt

~/codeFactory$ ll
total 8
drwx---r-x 2 user user   2 Aug 16 16:29  Download/
--------w- 1 user user 149 Aug 12 15:32  test.txt

How to Change Permissions in Numeric Code in Linux

You may need to know how to change permissions in numeric code in Linux, so to do this you use numbers instead of “r”, “w”, or “x”.

  • 0 = No Permission
  • 1 = Execute
  • 2 = Write
  • 4 = Read
~/codeFactory$ ll
total 8
drwx---r-x 2 user user   2 Aug 16 16:29  Download/
--------w- 1 user user 149 Aug 12 15:32  test.txt

~/codeFactory$ chmod 070 Download/

~/codeFactory$ chmod 700 test.txt

~/codeFactory$ ll
total 8
d---rwx--- 2 user user   2 Aug 16 16:29  Download/
-rwx------ 1 user user 149 Aug 12 15:32  test.txt*

Leave a comment