Changing Permissions of Files and Folders in Linux – Chmod Basics

The сhmod (change mode) command changes the access mode of files and directories.

Syntax

Syntax of the chmod command is the following:

$ chmod [options] permissions file[s]

Options:

  • -R, –recursive – Change files and directories recursively;
  • -f, –silent, –quiet – Suppress most error messages.

View the current file / directory mode using ls command:

$  ls -l MyFile.txt
-rw-r--r-- 1 john admin 0 2012-12-02 04:30 MyFile.txt
$ ls -ld MyDir
drwxr-xr-x 2 john admin 4096 2012-12-02 04:29 MyDir

or using stat command:

$  stat -c '%A %a %n' MyFile.txt
-rw-r--r-- 644 MyFile.txt
$ stat -c '%A %a %n' MyDir
drwxr-xr-x 755 MyDir

Classes

The references (or classes) are used to distinguish the users to whom the permissions apply. If no references are specified it defaults to “all”. The references are represented by one or more of the following letters:

Reference Class Description
u user the owner of the file (folder)
g group users who are members of the file’s (folders’s) group
o others users who are not the owner of the file (folder) or members of the group
a all all three of the above, is the same as ugo

Operators

The chmod program uses an operator to specify how the modes of a file (folder) 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

Modes

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

Mode Name Description
r read permitted to read the contents of file or directory (view files and sub-directories in that directory)
w write permitted to write to the file or in to the directory (create files and sub-directories in that directory)
x execute permitted to execute the file as a program/script or enter into that directory
X special execute applies execute permissions to directories regardless of their current permissions and applies execute permissions to a file which already has at least 1 execute permission bit already set (either user, group or other).

[X] Is not a permission in itself but rather can be used instead of x. It is only really useful when used with ‘+’ and usually in combination with the -R option for giving group or other access to a big directory tree without setting execute permission on normal files (such as text files), which would normally happen if you just used ‘chmod -R a+rx’, whereas with ‘X’ you can do ‘chmod -R a+rX’ instead.

Numerical permissions

You can use either the octal representation or symbolic representation to change the permission of a file or directory.

Octal representation for permissions:

  • First number is for user
  • Second number is for group
  • Third number is for others
# Mode Description
7 rwx read, write, execute
6 rw- read, write
5 r-x read, execute
4 r– read
3 -wx write, execute
2 -w- write
1 –x execute
0 no permissions

10 Simple Examples:

1. Read permission is added for all:

$ chmod a+r file

2. Execute permission is removed for all:

$ chmod a-x file

3. Change the permissions of the file to read and write for all:

$ chmod a+rw file

4. Read and write permissions are set for the owner, all permissions are cleared for the group and others:

$ chmod u=rw,go= file

5. Change the permissions of the directory and all its contents to add write access for the user, and deny write access for everybody else:

$ chmod -R u+w,go-w directory

6. Removes all privileges for all:

$ chmod file

7. Change the permissions of the file to read, write, and execute for all:

$ chmod 777 file

8. Sets read, write and no execution access for the owner and group, read only for all others:

$ chmod 664 file

9. Set a directory tree to ‘-rwx’ for owner directories, ‘-rw’ for owner files, ‘—‘ for group and others:

$ chmod -R u+rwX,g-rwx,o-rwx directory

10. Remove the execute permission on all files in a directory tree, while allowing for directory browsing:

$ chmod -R a-x+X directory
Was it useful? Share this post with the world!

Leave a Reply