Tuesday Tiny Techie Tip

chmod(1)

chmod has two different modes of operation. You can change the mode of a file explicitly by drawing an arcane picture of what you want the permissions to be, or you can use a slightly less arcane symbolic system.

The more arcane method uses three octal digits to show what the permissions should be. The octal numbers are created by associating each triplet of the file's permissions with a three-digit binary number which is then converted into a single-digit octal number.

Huh?

Let's stretch out a permission string so we can see what's going on:

user
permission
group
permission
other
permission
symbolic:
r
w
x
r
-
x
r
-
x
binary place:
4
2
1
4
2
1
4
2
1
binary:
1
1
1
1
0
1
1
0
1
octal:
7
5
5

So "rwxr-xr-x" can be thought of as three three-digit binary numbers with "r", "w", or "x" representing a "1", and "-" representing a "0". When you convert the three digit binary number to a one-digit octal number you get the following corresponcences:

symbolic binary octal
rwx
111
7
rw-
110
6
r-x
101
5
r--
100
4
-wx
011
3
These two aren't real useful
-w-
010
2
--x
001
1
---
000
0

So to change a file's permission to be "rw-rw-r--", you'd read that as three sets of bits "110 110 100", and convert those into octal to get "664" which you would use as an argument to chmod like so:


% ls -lg foo
-rwxr-xr-x  1 jeffy    se          24576 May  9 16:13 foo
% chmod 664 foo
% ls -lg foo
-rw-rw-r--  1 jeffy    se          24576 May  9 16:13 foo

The other method available in chmod is more symbolic and more flexible. You specify the permissions with an argument made up of three parts:

who
operation
permission
Where each part is one of:
uuser +add rread
ggroup -remove wwrite
oother =set xexecute

If the "who" portion is ommitted, the change is applied to all of user, group and other. It only makes sense to omit the permission portion with the "=" operator to set the permission to "---".

Here's a few examples:


% ls -lg foo  [I'll leave out the other ls's to reduce clutter]
-rw-rw-r--  1 jeffy    se          24576 May  9 16:13 foo
% chmod +x foo
-rwxrwxr-x  1 jeffy    se          24576 May  9 16:13 foo
% chmod go-rx foo
-rwx-w----  1 jeffy    se          24576 May  9 16:13 foo
% chmod g-w foo
-rwx------  1 jeffy    se          24576 May  9 16:13 foo
% chmod o=rx foo
-rwx---r-x  1 jeffy    se          24576 May  9 16:13 foo
% chmod g+rx,u-w foo
-r-xr-xr-x  1 jeffy    se          24576 May  9 16:13 foo

As in that last chmod, you can do multiple symbolic changes by separating them with a comma.

Of course there are other things you can do with chmod, like setting executables up to be setuid, and setting the sticky bit, but these are things you do seldom enough that it doesn't make sense to remember them. RTFM when you want to do it.


Tuesday Tiny Techie Tip -- 20 May 1997
Forward to (05/27/97)
Back to (05/13/97)
Written by Jeff Youngstrom

Up to the TTTT index

Tuesday Tiny Techie Tips are all © Copyright 1996-1997 by Jeff Youngstrom. Please ask permission before reproducing any of this material.