Files are another topic where GNU/Linux differs greatly from Windows and most other operating systems. We will cover the most obvious differences here, for more information see the chapter The GNU/Linux filesystem: ext2fs which offers greater detail.
The first difference, and probably the most important, is related to the presence of users. We could have mentioned that every user has their own directory (called his home directory), but this doesn't say what really goes on, which is that each file on a Unix system, is the exclusive property of one user and one group. Therefore, not only does a user have his own home directory, but he's also the owner of his files in the real sense of the word.
Moreover, permissions are associated with each file which only the owner can change. These permissions distinguish three categories of users: the owner of the file, every user who is a member of the group associated with the file (also called the owner group) but who is not the owner, and others, which means every user who is neither the owner nor the member of the owner group.
There are three different permissions:
Read permission (r) For a file, this allows its contents to be read. For a directory, this allows its contained files to be displayed, if and only if the execute permission is also set for this directory;
Write permission (w) For a file, this allows its contents to be modified. For a directory, it allows the files contained therein to be modified and deleted, even if the person is not owner of the directory but owner of the file they modify or delete;
eXecute permission (x) For a file, this allows for its execution (as a consequence, only executable files should normally have this permission set). For a directory, this allows a user to traverse it (which means going into or through that directory).
Every combination of these permissions is possible. For example: You can allow only yourself to read the file and forbid it to all other users, and forbid every other use of the file. You can even do the opposite, even if it's not very logical at first glance :-) As the file owner, you can also change the owner group (if and only if you are a member of the new group), and even deprive yourself of the file (that is, change its owner). Of course, if you deprive yourself of the file you will lose all your rights to it...
Let's take the example of a file and a directory. The display below represents entering the ls -l command from a command line:
$ ls -l total 1 -rw-r----- 1 queen users 0 Jul 8 14:11 a_file drwxr-xr-- 2 darth users 1024 Jul 8 14:11 a_directory/ $ |
The results of the ls -l command are (from left to right):
the first ten characters represent the type of file and the permissions for it. The first character is the type of the file: it contains a dash (-) if it's a regular file, or a d if it is a directory. There are other file types, which we will talk about in the Reference manual. The nine following characters represent the permissions associated to that file. Here you can see the distinction which is made between different users for the same file: the first three characters represent the rights associated to the file owner, the next three apply to all users belonging to the group but who are not the owner, and the last three apply to others. A dash (-) means the permission is not set;
following this are the number of links for the file. We will see in the Reference manual that the unique identification of files is not done by its name, but by a number (the inode number), and therefore it is possible under Unix that one file on the disk has several names. For a directory, the number of links has a special meaning, which we will also discuss in the Reference manual;
following this is the name of the file owner and then the name of the owner group;
finally, the size of the file (in bytes) and its last modification time are displayed, followed lastly by the name of the file or directory itself.
Let us now look closely at the permissions associated to each of these files: first of all, we must strip off the first character representing the type, and for the file a_file we get the following rights: rw-r-----. The interpretation of these permissions is as follows:
the first three (rw-) are the rights of the file owner, in this case queen. The user queen therefore has the right to read the file (r), modify its contents (w) but not execute it (-);
the next three (r--) apply to any user who is not queen but who is a member of the group users: such a user will be able to read the file (r), but neither write to nor execute it (--);
the last three (---) apply to any user who is not queen and is not a member of the users group: such a user will simply have no rights on the file at all.
For the directory a_directory, the rights are rwxr-xr--, and as such:
darth, as the directory owner, can list files contained inside (r), add or remove files from that directory (w), and he can traverse it (x);
each user who is not darth, but a member of the users group, will be able to list files in this directory (r) but not remove or add files (-), and will be able to traverse it (x);
every other user will have no rights on this directory. We have seen that the read permission alone on a directory doesn't allow a user to list the enclosed files, because even though this right is set, the execution permission is not (r--).
Remember, there is one exception to this rule. The root account can change the attributes (permissions, owner and group owner) of all files, even if he's not the owner. This means that he can also grant himself the ownership. He can read files on which he has no read permission, traverse directories which he would normally have no access to, and so on. And if he lacks a permission, he just has to add it.
In conclusion, we will mention a final distinction regarding filenames. They are more flexible and much less limited than under Windows:
they can contain any character (except the null character and a /), even non-printable ones. Because of this, you should be careful about case: the files readme and Readme are different, because r and R are two completely different characters;
as you may have noticed, a filename does not have to contain an extension unless you prefer it that way. Filename extensions do not identify the contents of files under GNU/Linux, unlike some other operating systems.