Linux Shell Scripting Cookbook(Third Edition)
上QQ阅读APP看书,第一时间看更新

How to do it...

In order to set permissions for files, we use the chmod command.

Assume that we need to set the permission, rwx rw- r-.

Set these permissions with chmod:

    $ chmod u=rwx g=rw o=r filename

The options used here are as follows:

  • u: This specifies user permissions
  • g: This specifies group permissions
  • o: This specifies others permissions

Use + to add permission to a user, group, or others, and use - to remove the permissions.

Add the executable permission to a file, which has the permission, rwx rw- r-:

    $ chmod o+x filename

This command adds the x permission for others.

Add the executable permission to all permission categories, that is, for user, group, and others:

    $ chmod a+x filename

Here a means all.

In order to remove a permission, use -. For example, $ chmod a-x filename.

Permissions can be denoted with three-digit octal numbers in which each digit corresponds to user, group, and other, in that order.

Read, write, and execute permissions have unique octal numbers, as follows:

  • r = 4
  • w = 2
  • x = 1

We calculate the required combination of permissions by adding the octal values. Consider this example:

  • rw- = 4 + 2 = 6
  • r-x = 4 + 1 = 5

The permission rwx rw- r-- in the numeric method is as follows:

  • rwx = 4 + 2 + 1 = 7
  • rw- = 4 + 2 = 6
  • r-- = 4

Therefore, rwx rw- r-- is equal to 764, and the command to set the permissions using octal values is $ chmod 764 filename.