chmodn

by Ray Patrick
(other software)

Convert UNIX file permission strings to octal chmod mode bit numbers.

Download:

chmodn is a single small shell script, so I don't bother hosting it on my Git server. Instead, you can view or download it here:

chmodn.sh (593 B)
MD5 sum: c894e01e5bda09c55a92dbfad1bffe5e


Embarassingly, I did not know about the built-in stat command when I wrote this script. This means that, aside from a learning experience (or a need to perform this operation in shell for whatever reason), this script is not truly necessary.

Name

chmodn - convert UNIX file permission strings to octal chmod mode bit numbers.

Synopsis

chmodn permission-string

Description

chmodn accepts a string containing UNIX file permissions (e.g. -rw-r--r--) and prints to standard output the octal number representing the bit pattern for those mode bits (e.g. 644).

UNIX permissions explained briefly

UNIX permission strings are 10-character strings that represent the privileges that users, groups, and others have for a particular file or directory. Users may have three privileges for any file or directory:

Let's examine the following example permission string:

drwxrwx---

The character d signifies that the file is actually a directory. This character can only appear in the leftmost column. Regular files have - in this field instead of d.

The chmod command accepts up to four octal digits corresponding to the permissions described above. The optional leading digit, when 4 digits are given, specifies special flags which I don't go into here. Each digit of the three rightmost digits represents a binary value corresponding to the state of the "read", "write", and "execute" permissions, respectively, where "1" means the permission is allowed and "0" means it is not.

Permission rwx Binary Octal
read, write, and execute rwx 111 7
read and write rw- 110 6
read and execute r-x 101 5
read only r-- 100 4
write and execute -wx 011 3
write only -w- 010 2
execute only --x 001 1
none --- 000 0

So, for our example above (drwxrwx---), the corresponding octal bit mode number would be 770:

permission: rwx rwx ---
binary: 111 111 000
octal:7 7 0
answer: 770
In other words, to set the permissions drwxrwx--- on the directory /home/foo/bar, you would execute $ chmod 770 /home/foo/bar.

Options

permission-string
A 10-character string representing UNIX-type file permissions. See the "DESCRIPTION" section for an explanation of the forms this string may take.

To Do

Author

Written by Ray Patrick in 2021.
https://raypatrick.xyz
ray@raypatrick.xyz

See Also

chmod(1)