Chmod modes: from symbolic to octal and back


Often you will be faced with permission limitations, that will hinder you from manipulating a certain file or directory. To overcome this on Unix-based systems, one can use the chmod command to edit permissions and enable more manipulations possibilities. In this blog, we examine the chmod command and its different modes.

Viewing file permissions

Chmod is part of the essentials packet coreutils, which comes as part of Ubuntu. Before getting to changing some file’s permissions, we need first to determine them. For that you can use the following command ls -l [filename] or -if you don’t want to see any extra unrelated information- use ls -l [filename] | awk {'print $1"    "$9'}.

Understanding file permissions

File permissions can be defined in two modes, each divided in 3 user categories:

  • Symbolic mode : In this mode each user category [u, g, o] and permission/right [r, w, x] is defined using a combination of letters, where r stands for read rights, w for write rights and x for execution rights.

  • Octal mode : In this mode the rights are defined using a three-digit octal number, where each digit represents the rights of a certain user category.

User category

Symbolic mode

Octal mode

Owner

u

  1. digit

Group

g

  1. digit

Other

o

  1. digit

all: Owner, Group and Other

a

Converting chmod permissions

Chmod changes the permissions of a given file/ directory according a to a rights description in a certain mode. A mode can be octal (description with numbers) or symbolic (description with letters). Whereas letters are easier to understand, octals are more practical and conversion from one mode to another can be done as follows:

  • \(\text{r = 4}\)

  • \(\text{w = 2}\)

  • \(\text{x = 1}\)

the \(\textbf{-}\) stands for not defined so in a way, you can use: \(\textbf{- = 0}\)

To give an example of how this conversion works. Assume you have the following permissions \(\text{-rwx-wx--x}\). First you divide the permission string in the three sub-groups after skipping the first character used to reflect the type of the file/ directory. For the resulting three groups, characters are substituted with their integer values and each group values are summed. The resulting code is:

  • \(\small{\text{rwx: r + w + x = 4 + 2 + 1 = 7}}\)

  • \(\small{\text{-wx: - + w + x = 0 + 2 + 1 = 3}}\)

  • \(\small{\text{--x: - + - + x = 0 + 0 + 1 = 1}}\)

so the resulting octal code is \(\text{731}\).

The following converter tool can be used to compute the symbolic/ octal code to describe a certain set of rules/ permissions:

Permissions:

Owner Group Other
Read
Write
Execute

Changing chmod permissions

In order to change the permissions of a file (file.sh for example) or directory using chmod, you can use any of the following commands:

  • In symbolic mode: chmod u=rwx,g=rw-,o=r-- file.sh

  • In octal mode: chmod 764 file.sh

One can also edit an already defined permission with the help of the following operators +, - and =. The following list includes some examples, that illustrate the use of those operators:

  • chmod a+x file.sh or chmod ugo+x file.sh or chmod +x file.sh allow file to be executed by all user categories (any user). So if the initial file permissions was -rw-rw-r--, after running the permissions, the resulting permissions is -rwxrwxr-x.

  • chmod o+w file.sh allow other users to write/ edit the file. So if the initial file permissions was -rw-rw-r--, after running the permissions, the resulting permissions is -rw-rw-rw-.

  • chmod g-w file.sh deny groups to write/edit the file. So if the initial file permissions was -rw-rw-r--, after running the permissions, the resulting permissions is -rw-r--r--.

  • chmod o=r file.sh allow other uses only to read the file.

  • chmod a-w file.sh deny write permission to everyone.

  • chmod go+rw file.sh make a file readable and writable by the group and other users. So if the initial file permissions was -rwx---r--, after running the permissions, the resulting permissions is -rwxrw-rw-.

Additionally, you can use the -R argument to change permissions recursively or combine chmod with the find command to change permissions of multiple files. For more on chmod check its manual using man chmod.

Conclusion

This blog post provided a small introduction to the chmod function used to change files/directories permissions. Moreover, the post included a description of the different modes used with chmod and how to convert from one to the other.

References and Further readings