Modify File Permissions with Linux chmod command (Ownership and Permissions)

Following are the symbolic representation of three different roles:

  • u is for user
  • g is for group
  • o is for others
  • a is for all

Following are the symbolic representation of three different permissions:

  • r is for read permission
  • w is for write permission
  • x is for execute permission

This is called octal notation because the binary numbers are converted to base-8 by using the digits 0 to 7:

  • 7, rwx, read, write, and execute
  • 6, rw-, read and write
  • 5, r-x, read and execute
  • 4, r–, read only
  • 3, -wx, write and execute
  • 2, -w-, write only
  • 1, –x, execute only
  • 0, —, none

The format of a chmod command

chmod [who][+,-,=][permissions] filename

There are two ways to give permissions.

Here is the equivalent command using octal permissions notation.

chmod 754 file.txt

This example uses symbolic permissions notation.

chmod u=rwx,g=rx,o=r file.txt

a+x is same as +x

chmod -R +w,g=rw,o-rw, ~/group-project-files/

Copying permissions

The parameter g=u means grant group permissions to be same as the user’s.

chmod g=u ~/myFile.txt

Making a File Executable

chmod +x ~/group-project.py

Add single permission to a file/directory

chmod u+x filename

Add multiple permission to a file/directory

chmod u+r,g+x filename

Remove permission from a file/directory

chmod u-rx filename

Change permission for all roles on a file/directory

Following code assigns execute privilege to user, group and others (basically anybody can execute this file).

chmod a+x filename

Make permission for a file same as another file (using reference)

file2’s permission will be set exactly same as file1’s permission.

chmod –reference=file1 file2

Apply the permission to all the files under a directory recursively

chmod -R 755 directory-name/

Change execute permission only on the directories (files are not affected)

chmod u+X *