Follow me on Twitter for my latest adventures!
There are two ways you can change file permissions in Unix - one is using chmod's symbolic (text) modes (like chmod ug+x file), the other is using the octal modes (like chmod 0660 file). It turns out that symbolic modes are more powerful because you can mask out the permission bits you want to change! Octal permission modes are absolute and can't be used to change individual bits. Octal modes are also sometimes called absolute because of that.
Here is an example that illustrates that.
Suppose you have this file,
$ ls -las file 0 -rw-rw---- 1 pkrumins cats 0 Feb 25 12:49 file
and you want to set the execute x bit for everyone. If you use symbolic modes, you can just do,
$ chmod a+x file $ ls -las file 0 -rwxrwx--x 1 pkrumins cats 0 Feb 25 12:49 file
But, if you're used only to absolute modes, then you first need to calculate the existing permission mask, which would be 0660, and then calculate the new one by adding the 1 to each group, so the end result would be 0771,
$ chmod 0771 file $ ls -las file 0 -rwxrwx--x 1 pkrumins cats 0 Feb 25 12:49 file
It's still easy with octal modes, but why do it the harder way when you can just chmod a+x file, right?
I'd also like to note that symbolic modes are a feature of chmod utility. If you look at chmod system call, it only takes the octal mode. The chmod utility first does a stat to determine the existing mode, then calculates the new one. It does the calculation for you. So don't do it again yourself!


Facebook
Plurk
more
GitHub
LinkedIn
FriendFeed
Google Plus
Amazon wish list
Comments
meow!
Remembers this :)
You can omit the `a` in `chmod a+x file`. I've never actually seen anyone include it before!
it's useful in the example, because if you try +w, it will *not* be the same as a+w...
It produces identical results on my slackware system. What is the difference you are implying?
Re-reading the chmod man page shows that a+... (or its variants a- & a= ) sets the flags to the settings, whereas the shortcut '+' sets the flags except for bits set in the umask.
I confirmed this by playing around. You can see your current umask with 'umask -p'.
My umask is 022 (like most, I think). Try 'chmod a+rwx file', then reset with 'chmod u=rw,go= file' then 'chmod +rwx file'. In the second case, the 'w' bit is not set for group and other users, despite being indicated in the command.
The practical effect of this is that 'chmod + etc' sets the flags but retains the default rwx settings, whereas 'chmod a+ etc' sets the flags irrespective.
Thanks Peter, for causing a useful discussion for what initially seemed like a very simple topic.
Another reason they are more powerful is the +X, which sets execute only if execute is set for other permission classes. This is incredibly useful when doing recursive chmods. Say I want to give group and other read write exec access to a directory that only user can access at current. You could do go+rwx, but that would make all files executable which is not what you want. If instead you do go+rwX, all of the dirs and actual executable files will be set to x (assuming you have access to all of the dirs).
I meant 777 in the above example.
I didn't know about +X.thanks John.
nice ..
Nice post. I also loves 'g+u' switch, it copies user permissions to group so groups have same permission as user. I love it because when working on machines where you are not allowed to give some permissions to everyone but only your group members then this is your savior.
Could it be that absolute mode is faster? For instance, if you are changing the permissions of a whole filesystem (to make it worse, imagine its a network filesystem), having to do a "stat" of the file before changing it makes it slower, right?
Also, i dont see why it is more powerful because u can do the same thing with both. Personally, I prefer absolute mode since my brain can calculate the mask faster than remembering the command flags.
There are also ACL's in chmod text mode but not in octal :D
It may be misleading to say that the text mode is more powerful. Actually, it depends on your use-case. For example, having seen the perms of a file, if all you wish to do is add/subtract perms, then obviously text mode is the way to go.
However, consider this use case. You `ls` a file (not necessarily with the `-l` option). AND, you wish to give it some absolute perms REGARDLESS of WHATEVER perms it currently has got. Then, if have been a *nix user for a while, you could simply use the number mode. Yes, you could employ the `=` operation of the text mode too, but it would be more verbose -- and as someone pointed out... slightly slower also -- than the number mode.
Leave a new comment