Understanding UNIX permissions and file types

I've also learned that you've got the following permissions:

I created a directory to test my newly acquired knowledge:

mkdir test 

Then I did some tests:

chmod u+rwx test/ # drwx------ chmod g+rx test/ # drwxr-x--- chmod u-x test/ # drw-r-x--- 

After fooling around for some time I think I finally got the hang of chmod and the way you set permission using this command.

But.

I still have a few questions:

But as I shouldn't be asking multiple questions, I'll try to ask it in one question.

In UNIX based system such as all Linux distributions, concerning the permissions, what does the first part ( d ) stand for and what's the use for this part of the permissions?

658 7 7 silver badges 19 19 bronze badges asked Feb 10, 2015 at 11:51 1,051 1 1 gold badge 11 11 silver badges 18 18 bronze badges

Incidentally, the value for the "d" is 040000 - it can be found in header files under the name S_IFDIR . You don't use it when setting the file mode, but the stat() function actually returns the value 040750 for drwxr-x--- .

Commented Feb 10, 2015 at 18:07

To your final question, I'd answer with another question: why do some people use octal permissions (e.g. 0777) instead of the much more comprehensible (ugo)(+-)(rwx)? Is it just the geek equivalent of a jacked-up pickup?

Commented Feb 10, 2015 at 18:31

@jamesqf Actually, now that I understand how the octal codes map, it's simpler for me to think about that way.

Commented Feb 10, 2015 at 22:12

@jamesqf it's much faster to type, when you're specifying the full set of permissions, than e.g. u=rwx,g=rx,o= for 750 .

Commented Feb 10, 2015 at 22:23

@Random832: But at least for me, any saving in typing speed is more than outweighed by the time needed to mentally translate between numbers and permissions. The more so as I seldom if ever want to set all permissions: I simply want to set execute or r/w permission. So about 90% of my chmod calls are just "chmod u+x".

Commented Feb 10, 2015 at 23:03

6 Answers 6

I’ll answer your questions in three parts: file types, permissions, and use cases for the various forms of chmod .

File types

The first character in ls -l output represents the file type; d means it’s a directory. It can’t be set or unset, it depends on how the file was created. You can find the complete list of file types in the ls documentation; those you’re likely to come across are

Permissions

chmod 0777 is used to set all the permissions in one chmod execution, rather than combining changes with u+ etc. Each of the four digits is an octal value representing a set of permissions:

The octal value is calculated as the sum of the permissions:

For the first digit:

See the chmod manpage for details. Note that in all this I’m ignoring other security features which can alter users’ permissions on files (SELinux, file ACLs. ).

Special bits are handled differently depending on the type of file (regular file or directory) and the underlying system. (This is mentioned in the chmod manpage.) On the system I used to test this (with coreutils 8.23 on an ext4 filesystem, running Linux kernel 3.16.7-ckt2), the behaviour is as follows. For a file, the special bits are always cleared unless explicitly set, so chmod 0777 is equivalent to chmod 777 , and both commands clear the special bits and give everyone full permissions on the file. For a directory, the special bits are never fully cleared using the four-digit numeric form, so in effect chmod 0777 is also equivalent to chmod 777 but it’s misleading since some of the special bits will remain as-is. (A previous version of this answer got this wrong.) To clear special bits on directories you need to use u-s , g-s and/or o-t explicitly or specify a negative numeric value, so chmod -7000 will clear all the special bits on a directory.

In ls -l output, suid , sgid and “sticky” appear in place of the x entry: suid is s or S instead of the user’s x , sgid is s or S instead of the group’s x , and “sticky” is t or T instead of others’ x . A lower-case letter indicates that both the special bit and the executable bit are set; an upper-case letter indicates that only the special bit is set.

The various forms of chmod

Because of the behaviour described above, using the full four digits in chmod can be confusing (at least it turns out I was confused). It’s useful when you want to set special bits as well as permission bits; otherwise the bits are cleared if you’re manipulating a file, preserved if you’re manipulating a directory. So chmod 2750 ensures you’ll get at least sgid and exactly u=rwx,g=rx,o= ; but chmod 0750 won’t necessarily clear the special bits.

Using numeric modes instead of text commands ( [ugo][=+-][rwxXst] ) is probably more a case of habit and the aim of the command. Once you’re used to using numeric modes, it’s often easier to just specify the full mode that way; and it’s useful to be able to think of permissions using numeric modes, since many other commands can use them ( install , mknod . ).

Some text variants can come in handy: if you simply want to ensure a file can be executed by anyone, chmod a+x will do that, regardless of what the other permissions are. Likewise, +X adds the execute permission only if one of the execute permissions is already set or the file is a directory; this can be handy for restoring permissions globally without having to special-case files v. directories. Thus, chmod -R ug=rX,u+w,o= is equivalent to applying chmod -R 750 to all directories and executable files and chmod -R 640 to all other files.