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!

Comments

February 27, 2011, 23:28

meow!

February 28, 2011, 10:40

Remembers this :)

February 28, 2011, 11:06

You can omit the `a` in `chmod a+x file`. I've never actually seen anyone include it before!

Homer Permalink
February 28, 2011, 11:57

it's useful in the example, because if you try +w, it will *not* be the same as a+w...

Nick Coleman Permalink
February 28, 2011, 14:36

It produces identical results on my slackware system. What is the difference you are implying?

Nick Coleman Permalink
March 01, 2011, 01:26

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.

John Permalink
February 28, 2011, 13:02

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).

John Permalink
February 28, 2011, 13:05

I meant 777 in the above example.

hemant Permalink
February 28, 2011, 16:55

I didn't know about +X.thanks John.

hemant Permalink
February 28, 2011, 16:54

nice ..

Avadh Permalink
February 28, 2011, 17:22

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.

Dani Permalink
March 02, 2011, 03:13

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.

tjado Permalink
March 03, 2011, 17:19

There are also ACL's in chmod text mode but not in octal :D

Harry Permalink
March 05, 2011, 11:18

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.

Jamne Permalink
April 11, 2014, 10:22

Thank you assistant about this great article and would like to appreciating and http://www.globalwritings.com/blog/custom-paper this is helpful in studies online.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "computer_253": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements