File Basics

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:

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

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

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

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.

For the directory a_directory, the rights are rwxr-xr--, so:

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:

Note

However it is worth noting that many graphical applications (file managers, office applications, etc.) actually use file extensions to recognize files. It is therefore a good idea to use file name extensions for those applications which support it.