Compared to Windows and most other operating systems, files are handled very differently under GNU/Linux. In this section we will cover the most obvious differences. For more information, please read Chapter 9, The Linux File System.
The major differences result directly from the fact that Linux is a multiuser system: every file is the exclusive property of one user and one group. One thing we did not mention about users and groups is that every one of them possesses a personal directory (called the home directory). The user is the owner of this directory and of all files he/she creates.
However, this would not be very useful if that were the only notion of file ownership. As the file owner, a user can set permissions on files. These permissions distinguish between three user categories: 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 includes every other user who is neither the owner nor a member of the owner's group.
There are three different permissions:
Read permission (r): enables a user to read the contents of a file. For a directory, the user can list its contents (i.e. the files in this directory).
Write permission (w): allows the modification of a file's contents. For a directory, the write permission allows a user to add or remove files from this directory, even if he/she is not the owner of these files.
eXecute permission (x): enables a file to be executed (only executable files normally have this permission set). For a directory, it allows a user to traverse it, which means going into or through that directory. Note that this is different from the read access: you may be able to traverse a directory but still be unable to read its contents!
Every permission combination is possible. For example, you can allow only yourself to read the file and forbid access to all other users. You can even do the opposite, even if it's not very logical at a 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 the command line:
$ ls -l total 1 -rw-r----- 1 queen users 0 Jul 8 14:11 a_file drwxr-xr-- 2 peter 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 file's type and the permissions associated with it. The first character is the file's type: if it is a regular file, it will contain a dash (-). If it's a directory, the leftmost character will be a d. There are other file types, which we will talk about in the Reference Manual. The next nine characters represent the permissions associated with that file. The nine characters are actually three groups of three permissions. The first group of three characters represent the rights associated to the file owner, the next three apply to all users belonging to the same group but who are not the owner, and the last three apply to others. A dash (-) means that the permission is not set.
Next comes the number of links for the file. We will see in the Reference Manual that the unique identifier of a file is not its name, but a number (the inode number), and that it is possible for one file on disk to have several names. For a directory, the number of links has a special meaning, which will also be discussed in greater detail in the Reference Manual.
The next piece of information is the name of the file owner and the name of the owner group.
Finally, the size of the file (in bytes) and its last modification time are displayed, with the name of the file or directory itself being the end of the line.
Let's take a closer look at the permissions associated with 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-----. Here's a breakdown of the permissions.
the first three characters (rw-) are the file owner's rights, which in this case is queen. Therefore, queen has the right to read the file (r), to modify its contents ( w) but not to execute it ( -).
the next three characters ( r--) apply to any user who is not queen but who is a member of the users group. They will be able to read the file (r), but not write or execute it (--).
the last three characters ( ---) apply to any user who is not queen and is not a member of the users group. Those users won't have any rights on the file at all.
For the directory a_directory, the rights are rwxr-xr--, so:
peter, as the directory owner, can list files contained inside (r), add to or remove files from that directory (w), and can traverse it (x)
each user who is not peter, 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 only be able to list the contents of this directory (r). Because they don't have wx permissions, they won't be able to write files or enter the directory.
There is one exception to these rules: root. root can change attributes (permissions, owner and group owner) of all files, even if he's not the owner, and could therefore grant himself ownership of the file! 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. root has complete control of the system, which involves a certain amount of trust in the person wielding the root password.
Lastly, it is worth noting the differences between file names in the UNIX and the Windows worlds. For one, UNIX allows for a much greater flexibility and has fewer limitations:
a file name may contain any character, including non-printable ones, except ASCII character 0, which is the end of a string, and /, which is the directory separator). Moreover, UNIX is case sensitive: the files readme and Readme are different, because r and R are considered two different characters under UNIX-based systems.
As you may have noticed, a file name does not have to include an extension, unless that's the way you prefer to name your files. File extensions do not identify the contents of files under GNU/Linux or almost any other operating system. So-called “file extensions” are quite convenient though. The period (.) under UNIX is just one character among others, but it also has one special meaning. Under UNIX, file names beginning with a period are “hidden files”, which also includes directories whose names start with a .