Managing file permissions with chmod using numbers

The chmod command can also be used to modify file or directory permissions, using the numeric method. This method sets exactly the new permissions specified, using numbers with the chmod command, irrespective of the existing permissions of the file or directory. In the numeric method, permissions are represented by three-digit (or four-digit when setting special permissions) octal numbers. Each octal number represents a number between zero and seven, which is the sum of permissions for an access level—either a user, group, or others. The syntax to apply permissions using numbers is as follows:

$ chmod     ###     file   |    directory

Here, each # represents permissions for a user, group, and other access levels, starting from left to right. It is the sum of all the permissions (read, write, and execute) of that access level, where each permission has a certain numeric weightage, as follows:

Here is a table listing the numeric weightage of permissions:

Permission

Numeric weightage

Read (r)

4

Write (w)

2

Execute (x)

1

 

When setting permissions using the numeric method, we calculate the total sum of permissions we need for the user, group, or others; for example, if you want to use set-rwx-r-xr-- as the new permission for file1, you do the following:

user → rwx → 4+2+1=7

group → r-x → 4+1=5

others → r- - → 4=4

The following example of the chmod command shows the usage of the chmod to set permissions using numbers and the usage of chmod to set the same permissions using symbols:

$ chmod 754 file1                
$ chmod u=rwx,g=rx,o=r file1

The following are some examples to illustrate the usage of the chmod command using numbers:

Set the read permission for the user, the write permission for the group, and no permission for others on the file named file1, as shown in the following screenshot:

Use chmod with the -R option to recursive set permissions on all files and sub-directories within a directory. With the X option, you can add or remove execute permissions for directories only and not files. Although not used much, it is a useful option sometimes; you can use chmod -R +rX /data to set execute permissions for directories in the /data folder, but not for any file within it.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.38.92