5. The File System

$Id: filesystem.xml 2331 2009-04-18 12:59:52Z rafi $

Table of Contents

5.1. The Home Directory
5.2. The ls Command
5.3. File and Directory Commands
5.4. File and Directory Permissions
5.5. Special Stuff


File and directory names are case-sensitive in UNIX. For instance, the file names figures.txt and Figures.txt are different file names when it comes to UNIX.

The file system is the place where files are stored. Files can be grouped in directories (also known as “folders”). Directories are hierarchically organized, which can be thought of as a tree (see Figure 1, “An excerpt of the directory structure of the FreeBSD operating system”). The start point, or top-level directory of the file system is called root (not to be confused with the user also called “root”). Everything else is a sub-directory of the root directory. Sub-directories can have an arbitrary number of other sub-directories, and so on.

Figure 1. An excerpt of the directory structure of the FreeBSD operating system

|-- bin
|-- etc
|   |-- X11
|   |-- defaults
|   |...
|   `-- rc.d
|-- home
|   |-- william
|   `-- averell
|-- lib
|-- sbin
|-- usr
|   |-- X11R6
|   |-- bin
|   |-- doc
|   |-- include
|   |-- lib
|   |...
|   |-- local
|   |-- ports
|   |-- sbin
|   |-- share
|   `-- src
`-- var
    |-- account
    |-- log
    |-- mail
    |-- run
    |-- spool
    |-- tmp
    `-- yp

To put it plain and simple, directories are like chapters in a book, sub-directories are sub-chapters, which can be even further divided in sub-sub-chapters etc. The root directory is the book itself, and files would be paragraphs of chapters.

To reference a directory, you use a path. There are two types of paths

absolute paths
which always start at the root, for instance /usr/sbin
relative paths
which are relative to the current directory, for instance sbin

The slash / is used to separate directory names in a path. The path /usr/share/doc, for instance, points to the root directory (“/”), in there to the usr directory. The next component points to the share directory which is a sub-directory of /usr. The last component eventually points to the doc directory in /usr/share.

As you have seen, the root directory is not named “root” but is only the single / character. As opposite to a Microsoft based operating system where for each drive a separate directory tree exists, UNIX has only one directory tree. Everything else is part of that directory tree. Thus, you cannot be sure whether or not the directory you are currently in is really on your computer. But you do not need to bother either.

For you as user, the most important directories are /bin, /usr/bin, and /usr/local/bin. These are the directories most commands you work with are stored in. There may be other directories, but that depends on the operating system. Under Sun Solaris, for instance, the directories /usr/sfw/bin and /opt/sfw/bin hold some useful commands as well.

You are always in a directory. Sometimes it is not so obvious, sometimes it is more obvious. Thus there is an important term current working directory (cwd). The current working directory is what its name says, the directory you are working in.

In every directory, there are always two special directories. One is simply named . (one dot), and the other .. (two dots). Two dots stands for the parent directory of the directory they are in. So, for example, if you are in the directory /usr/bin, the two dots point to /usr (see Example 16, “The two dots (..) directory”).

Example 16. The two dots (..) directory

The cd command is used to change directories, and pwd is used to show the personal working directory which is equivalent to cwd (see also Section 5.3, “File and Directory Commands” for an explanation of those two commands).

$ pwd
$ cd ..
$ pwd

5.1. The Home Directory

Each user has a home directory. If you do a fresh login, you will most likely find yourself in your home directory. Under Linux and FreeBSD the home directories are located under the /home directory. On Sun Solaris they are kept in the /export/home directory. Regardless of the system, they are called the same as your user name. If the user name is “joe”, the full path of the home directory would be /home/joe under FreeBSD and Linux and /export/home/joe under Sun Solaris.

The home directory is your directory, and you are by default the only person having access to it (of course, root has access, too). Programs store their settings in that directory, and you should use it to store your data, unless advised otherwise by your UNIX administrator.

5.2. The ls Command

If you want to know the content of a directory, you'll want to use the ls. It lists the content the of the directory you specified, or the content of the current working directory if you don't specify a directory

$ ls
AdobeFnt.lst       data               example.org        schemas.xml~
agent-info         Desktop            libwindow          SunStudioProjects
analysis1.tex      Documents          media              test
bin                files              pgadmin.log        unixsurvivalguide
cert               GNUstep            schemas.xml
$ ls /tmp
0_sun_studio_12                    mc-jack
gpg-X0ygPY                         ogl_select341
hsperfdata_noaccess                opera-9.63-2474.gcc3-static-qt3
hsperfdata_root                    opera-9.63.gcc3-static-qt3.tar.gz
kde-jack                           ssh-YZysV664

May be you want some more information on the files, you could try the -l option, which produces something like that

$ ls -l
-rw-r--r--   1 jack     jack         494 Dec 28 22:05 abstract.xml
-rw-r--r--   1 jack     jack         115 Dec 28 22:00 commands.xml
drwxr-xr-x   4 jack     jack          27 Dec 28 22:04 docbook
-rw-r--r--   1 jack     jack        6496 Dec 28 22:18 filesystem.xml
-rw-r--r--   1 jack     jack         852 Dec 28 22:05 Makefile
-rw-r--r--   1 jack     jack         166 Dec 28 22:05 processes.xml
-rw-r--r--   1 jack     jack         123 Dec 28 22:00 terminal.xml
-rw-r--r--   1 jack     jack       59645 Dec 28 22:18 unixsurvivalguide.fo
-rw-r--r--   1 jack     jack        9566 Dec 28 22:18 unixsurvivalguide.html
-rw-r--r--   1 jack     jack        1195 Dec 28 22:05 unixsurvivalguide.xml

The table below explains the columns printed by the ls using the -l switch (from left to right).

Table 2. The output of ls -l explained.

Column #Description
1 The permission of the directory or file as discussed in Section 5.4, “File and Directory Permissions”. However, the first character describes the type of the directory item. If it shows the letter d the entry describes a directory. Is a dash (-) printed, the entry in question is a regular file.
2 The meaning of the number in depends of the type of the entry. If the entry is a directory, that is the number of files (and only files) in the directory. Should the entry happen to be a file, that number represents a count of hard-links to that file (see also Section 5.5, “Special Stuff”).
3 Name of the user that owns that file or directory. For each directory or file, there can be only one owner. The owner can be changed by the owner himself or root, using the chown command (see Section 5.4.1, “Commands” for further details on chown).
4 The name of the group that has access to the file or directory. As you can see by the example above, it is possible for groups to have the same name as users. The system can distinguish between them. The group ownership can only be changed by the owner of the file or root using the command chgrp (see Section 5.4.1, “Commands” for further details on chgrp).
5 The size in bytes of the file or directory. You might want to see that in a human readable fashion. Try ls -lh and the size will be printed nicely in units of bytes, kilo bytes, mega bytes, and giga bytes as appropriate.
6 The time and date of the last modification.
7 The name of the file or directory.

In order to display hidden directories and files (see Section 5.5, “Special Stuff”), call ls with the -a option. Each file or directory starting with a dot is hidden directory or file. Hidden directories and files are not displayed when invoking ls without the -a switch.

5.3. File and Directory Commands


The commands explained below will work on the current working directory if you don't specify an absolute path.


Returns the current working directory, for instance

$ pwd

Refer to Section 5.2, “The ls Command”.


The most important command right next to ls. It allows to change the current directory.

$ pwd
$ cd /usr/bin
$ pwd

You can use relative and absolute paths when invoking cd. For the following examples, we assume the current working directory is /home/joe. In /home/joe exist the two directories tmp and bin.

To change to the bin directory, there exist to ways: the longer way is to specify the absolute path

$ cd /home/joe/bin

and the probably preferred way using the relative path

$ cd bin

Both ways will change the directory to /home/joe/bin.

Suppose, you are still in the /home/joe/bin directory and you want to change to the /home/joe/tmp directory. Again, you have two choices: using the absolute path, you would enter

$ cd /home/joe/tmp

or the easy way

$ cd ../tmp

With the mv command you can move files or directories from one directory to another. It is also used to rename files or directories.

$ mv <source> <destination>

Replace <source> with the file or directory name to be moved. <destination> is the destination where the file is moved to. If it does not exist, mv assumes you want to rename the file or directory.


If the destination exists, it will be overwritten without confirmation.


The cp copies files. It has the same syntax as the mv command. If you want to copy a directory, use the -r switch as shown in the example below.

$ cp -r data/ /home/joe


If the destination exists, it will be overwritten without confirmation.


rm is used to delete files. You can only delete a file if you have write permission to the directory the file is in. Else you would see an error message like that

$ rm figures
rm: figures not removed: Permission denied

You can also delete directories with the rm providing the -r switch. See the example below.

$ rm -r olddata


You must be careful using the -r switch of rm. It deletes everything in the directory specified including sub-directories without any confirmation question, such as known by Microsoft operating systems. So you once fired on the wrong directory, you have little chance of stopping it. Especially since the rm command deletes the files for real, and does not move them to some recycle bin or trashcan, where you can salvage the files from.


mkdir creates a new directory.

$ mkdir <dirname>

Replace the <dirname> with the name of the new directory to be created.


With the rmdir you can delete an empty directory. The syntax is the same as the one of mkdir.

5.4. File and Directory Permissions

Table of Contents

5.4.1. Commands

We look again at the output of the ls invoked with the -l option and pay only attention to the first column as shown in Example 17, “Permission bits”.

Example 17. Permission bits

-rw-r--r--   1 joe     users         494 Dec 28 22:05 somefile
drwxr-xr-x   4 joe     users          27 Dec 28 22:04 docbook

The permission bits are displayed in the first column printed by ls -l.

Not taking the first character into acount, the first column can be divided into three groups (triplet) of three characters each. The first triplet describes the permissions of the entry applying to the owner of the entry, the second the permission applying to the group owner. The last triplet applies to anybody not covered by the first two triplets, or the “other” or “world” in UNIX speak.

In the example above, the user joe has set the permission to rw-, the group users to r--, and the world to r--.

The first character of a triplet indicates whether or not the entry can be read. If a r appears, the bit is set and thus the entry can be read. A dash would indicate that the bit has not been set. The second character controls the write access to an entry. A w stands for write access granted, and the dash means the bit is not set. The third and last character of a triplet specifies whether or not the entry can be executed by the operating system. Depending of the type of the entry (file or directory) its meaning is different. If this bit is set for a file, it indicates that file can be executed by the operating system. For example, commands have this bit set. If the entry is a directory, the execution bit tells UNIX that users are allowed to change to this directory. But only changing is allowed. If only the execution bit is set on directory, you can do a cd to it, but you wont be able to read the contents of the directory.

See Example 18, “Permission bits revisited” for some examples of permission bits.

Example 18. Permission bits revisited

-rw-r--r--   1 joe     users

The user joe has read and write access to the file. The group users and world have read access to the file.

-rw-rw-r--   1 root     staff

Root and the group staff have read and write access to the entry. World has read-only access.

drwxr-xr-x   1 root     users

Root has read and write access to the directory. The group users and world can read the directory. Root, users, and world are allowd to change to the directory.

-rwxr-xr-x   1 root     bin

Root has read and write access to the file. The group bin and world have read-only access. Root, bin, and world can execute the file.

5.4.1. Commands


chmod lets you change the permission bits of files and directories. I will explain only how to use the absolute-mode, where you specify a number representing the access bits. Refer to the man page of chmod for more details.

In absolute-mode, the number 1 is used to set the execution bit, 2 for write access, and 4 for read access. You can combine the mode by adding the numbers, e.g. 6 would mean read and write access, 5 read and execute permission.

With those numbers you construct a three digit number. The first digit represents the permissions for the owner, the second digit the permission for the group, and the third the permission for world. See Example 19, “chmod using absolute-mode”.

Example 19. chmod using absolute-mode

Issuing the command

$ chmod 774 somefile

would set the permission for the owner and the group of the file somefile to read, write, and execute. World would have read access only.


The chown lets you change the owner of a file. You are only allowed to do so, if you are the owner. You cannot take over the ownership of a file you don't already own.

The syntax of the chown command is simple:

$ chown <user> <filename>

Replace <user> with the name of the new owner and <filename> with the file or directory name of which the owner has to be changed.


chgrp is used in the same manner as chown. It will change the group owner of the file specified. You may only change the group owner if you are the owner of the file or directory.

5.5. Special Stuff

5.5.1. Hidden Files

It is a convention, that you can hide a file or directory under UNIX by prepending a dot in front the name. This is often used for configuration files in your home directory. You can list hidden files by invoking ls with the -a option as shown in Example 20, “List hidden files and directories”.

Example 20. List hidden files and directories

$ ls -a
.                             .padminrc
..                            .pgadmin3
.acrorc                       .pgpass
.adobe                        .printers
.alias                        .purple
.aspell.en.prepl              .qt

The entries with a dot in front of the name are hidden (except, of course, the first two entries in the left column).

But you cannot arbitrarily hide files by simply putting a dot in front of the name. The dot is part of the file name and thus you would change the file name. If you want to reference to a hidden file or directory, you have to include the leading dot as well.

5.5.2. Links

You can create links to files and directories. From a user's point of view, a link is no different to a file or directory, but they can be very convenient. You could, for example, create links to often used directories in your home directory. This would save you from always typing in the whole path.

UNIX knows two types of links, namely hard-links and soft-links. I won't discuss hard-links here, because they are somewhat limited.

You can create soft-links to files and directories a shown in Example 21, “Creating soft-links”. Make sure you don't omit the -s switch. Else you would create a hard-link.

Example 21. Creating soft-links

$ ln -s <sourcefile> <linkname>

Replace <sourcefile> with the file or directory name of the item the link has to point to. The name of the link is specified by <linkname>.

When using the -l with ls, you will spot soft-links easily, as shown in Example 22, “Spotting soft-links with ls”.

Example 22. Spotting soft-links with ls

$ ls -l
total 72
-rw-r--r--   1 jack     jack        6144 Jan  7 15:08 file1.txt
-rw-r--r--   1 jack     jack        1024 Jan  7 15:07 file12.txt
-rw-r--r--   1 jack     jack       15360 Jan  7 15:07 file13.txt
-rw-r--r--   1 jack     jack        5120 Jan  7 15:07 file2.txt
lrwxrwxrwx   1 jack     jack           9 Jan  7 15:08 justalink -> file1.txt

The last line of the output clearly shows that justalink is a soft-link to file1.txt.

Links can be removed using the rm command like regular files.