10

I was recently asked by a colleague to use chmod with letters instead of numbers. Apart from the obvious readability advantage is there any particular reason to use letters over numbers ?

2 Answers 2

16

The chmod symbolic notation is more fine-grained compared to the octal notation, allowing the modification of specific mode bits while leaving other mode bits untouched.

The symbolic notation consists of three components:

chmod [references][operator][modes] file

The references consists of a combination of the letters ugoa, which specify which user's access to the file will be modified: the user who owns it (u), other users in the file's group (g), other users not in the file's group (o), or all users (a). If the references component is omitted, it defaults to all users, but only permissions allowed by the umask are modified.

The + operator causes the specified file mode bits to be added to the existing file mode bits of each file; - causes them to be removed; and = causes them to be added and unspecified bits to be removed, except setuid and setgid bits set for directories, unless explicitly specified.

The mode consists of a combination of the letters rwxXst, which correspond to the read (r), write (w), execute (or search for directories) (x), execute/search only if the file is a directory or already has execute permission for some user (X), setuid or setgid (depending on the specified references) (s) and restricted deletion flag or sticky bit (t). Alternatively, the mode can consist of one of the letters ugo, in which case case the mode corresponds to the permissions currently granted to the owner (u), member's of the file's group (g) or permssions of users in neither of the preceding categories (o).

Examples

Assuming the permission set for file is 0764/-rwxrw-r--

Remove permission from other users not in file's group:

  • Octal: chmod 760 file

    Note how the existing permissions left unchanged must be repeated when using the octal notation.

  • Symbolic: chmod o-rwx file

    With symbolic notation, the existing file permissions do not matter.

Set setuid:

  • Octal: chmod 4764 file

  • Symbolic: chmod u+s file

Set setgid:

  • Octal: chmod 2764 file
  • Symbolic chmod g+s file
1

Using the letters allows you to toggle individual bits on and off without changing the other bits.

Assume I have the following directory:

drwxrwsr-x   6 f261-01a  g261-01a  4096 Sep 12 17:51 /home/teams/261/f261-01a

If I want to turn off read permission for other and I want to use numbers, I might just quickly do chmod 751. Except I've just turned off the setgid bit on the directory. Oops.

If, however, I use chmod o-r, I only toggle off the read bit I wanted and the setgid bit on the directory stays set.

It's also useful if you want to turn on setuid, setgid or sticky bits on a file or directory. Especially if you do it so seldom you have trouble remembering which bit is which in the 4th octet.

So, it depends what you are doing. I still prefer using numbers when I am just setting permissions and I don't care what is there to begin with. But if you are toggling individual bits, the letter notation is much easier.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .