Linux Chmod Command Tutorial with Examples To Change Permission of Files and Folders – POFTUT

Linux Chmod Command Tutorial with Examples To Change Permission of Files and Folders

chmod command is used to change access permission of files and directories in Linux operating systems. chmod stands for change mode. Access permissions specify whether a user account or group can read, write, or execute a given file and directory.

chmod Command Syntax

Syntax of chmod command is like below.


Linux File and Folder Access Roles

While managing access permission for files and directories we should specify the roles. Permissions are set and bind to the given roles. In Linux following roles exist for access permissions.

  • owner specifies the file or directory owners. It can be listed with ls -l command.
  • group specifies the file or directory group owners. Files and directories group owners can be listed with ls -l.
  • other specifies all user accounts and groups which means all other world. While giving privilege to this role be cautious.

Linux File and Folder Access Modes

In Linux, there are three access modes. Access modes specify the way given user or group access to the file or directory.

  • read mode specifies reading file or directory.
  • write mode specifies writing and changing file or directory
  • execute mode specifies executing file

List File and Folder Access Permissions

Before starting to change permissions we need to list current roles and permissions about files and directories. We will use ls -lcommand to list permissions. In this example we will list all current working directory file and folders permissions.

$ ls -l
List Permissions
List File and Folder Access Permissions

We can see that owner user and group is ismail and the first column like --rw-r--r-- shows user, group and other privileges.

  • - is user or group bit
  • rw- is owner permissions. Here read and write permission given
  • r-- is group permission. Only read permission given
  • Last r-- is other permission. Only read permission given.
LEARN MORE  For Loops In Windows With MS-DOS Command Prompt

Change File and Folder User Access Permission

Now those provided information to understand file and directory permissions is enough. We will change single directory permission in this example. We will change user permission of file named ping.txt . We will use u to specify user. + means add permission. x mean execute permission.

$ chmod u+x ping.txt

Change File and Folder Group Access Permission

We can change a file or directory permission with g option. In this example, we will remove the group read permission of file ping.txt .

$ chmod g-r ping.txt
  • g used to specify group
  • - is used to remove given permission
  • r is used to specify read permission

Change File and Folder Multiple Group and User Access Permissions

We can change in a single chmod command multiple roles permissions. We will just delimit them with a command.

$ chmod g-r,u+x ping.txt

Change File and Folder Access Permission Recursively

Changing file and directory permissions one by one is a trivial task. We can use -r option to take effect all subdirectories recursively. In the following example, we change all given directory named test and its subdirectories permissions.

$ chmod -r g-r test

Add File and Folder Access Permission

We have already seen but I want to emphasize add permission operation. + between role and permission used to add permission to the given role. In this example we add permission to the user to execute ping.txt

 $ chmod u+x ping.txt

Remove File and Folder Access Permission

- between role and permission is used to remove permission for the given role. In this example we remove users execute permission from file ping.txt

$ chmod u-x ping.txt

Copy Permissions From Other File

If we have already set some file permissions we can use this file as a reference point for permission. We can copy given file permissions to the specified file. We will use --reference option and the reference file name. In this example, we will use run file permissions as a reference to apply to run2 .

$ chmod --reference=run run2

Using Numeric Access Permissions

Up to now we have user u , g and o for roles and x , w ,r for permissions. There is another presentation for these. We will use three digits for u ,g , o role specification like 540 . Here 5 is for user , 5 is  for group 0 for others.

  • r value is 4
  • w value is 2
  • x value is 1

So if we want to give r and w we will sum their numeric values which is 6 .

LEARN MORE  Javascript Define and Call Functions

Here some examples

  • x w equal to 3
  • r x equal to 5

Using Numeric Values For Chmod

We can use previously explained numeric values for chmod . We will only provide related value to the command. In this example we only want the user to read , write, and execute permissions and others not.

$ chmod 700 executable

Give All Permissions To All Roles

We can give all permission to all roles which means user, group and others can read, write and execute. This is very insecure and dangerous action. We will use 777

$ chmod 777 file

2 thoughts on “Linux Chmod Command Tutorial with Examples To Change Permission of Files and Folders”

  1. r value is 1
    w value is 2
    x value is 4

    sir i think it should be
    r value is 4
    w value is 2
    x value is 1


Leave a Comment