Tuesday, December 20, 2011

Octal Permissions Pro Tip


For Linux and Linux like OS, file permissions can be set with octal (0-7) using chmod and umask. umask works differently than chmod by setting default permissions of newly created files and directories by subtracting permissions. I won't go into detail about this now. This page assumes you know the basics of those file permissions. You may have seen chmod 755 or similiar many times. How can we know what each number means? Memorize? Maybe, but there's a better way.

Easy Method

You only need to remember this:

4 = read 2 = write 1 = execute

Notice I did not include 0; because that is easy to remember that it means no permissions. Now all you need is simple addition to figure out a combination of permissions. For example, the 5's in 755 means

4 (read) + 1 (execute) = 5 (read and execute)

Now if you want for example read and write permissions only, just follow the simple addition again.

4 + 2 = 6

To verify this is true, I will show you the results on my machine.

[bull@beastlinux bin]$ chmod 600 vpaste

[bull@beastlinux bin]$ ls -l vpaste

-rw-------. 1 bull bull 355 Dec 19 03:54 vpaste

[bull@beastlinux bin]$

Another note:

To help remember, 4, 2, 1 which is which. They go in the same order as shown -rwx------. The order is read (4), write (2), execute (1).


Happy chmodding =)