by Ray Patrick
(other software)
Convert UNIX file permission strings to octal chmod
mode bit numbers.
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.
chmodn - convert UNIX file permission strings to octal chmod
mode bit numbers.
chmodn permission-string
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 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:
r
) permission allows a user to access the file.w
) permission allows a user to modify the file.x
) permission allows a user to run the file (if it is a script or program).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
.
d
, rwx
, define permissions for the user class (i.e. the file owner).rwx
, defines permissions for the group class (i.e. the group owning the file).---
, define permissions for the "others" class. In this example, users who are not the file owner and are not members of the group (and are thus in the "others" class) have no permissions for this file.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: 770In other words, to set the permissions
drwxrwx---
on the directory /home/foo/bar
, you would execute
$ chmod 770 /home/foo/bar
.
Written by Ray Patrick in 2021.
https://raypatrick.xyz
ray@raypatrick.xyz
chmod(1)