Personal tools
You are here: Home Documentation Unix Basics The File System

The File System

The Unix file system consists of a tree of directories, each of of which may contain files and other directories. The lowest directory of this hierarchy is called the "root" directory. Your Unix "shell" has a notion of your "current" directory. You can change that any time by the "cd" ("change directory") command. When your account was set up a 'home' directory was created for you, where you may create your own files and subdirectories. Each time you log in, your current directory is set to this home directory.
There are several ways to specify a file somewhere in the directory tree. Let's look at an example:

tree2

This is a very simplified view of a unix directory tree. The root directory (denoted by "/") contains the subdirectories "etc", "usr", "u" "cray" and "afs" in this example. Most system software, configuration files and libraries reside in subdirectories of "etc" and "usr". The user directories are kept in the "u" directory (on some machines this is called "home" or something else). Your home directory is called "me" in the figure above. (usually identical to your ID). There is just one file "baz" and two subdirectories "c" and "fortran", each of which contains a single file. If you just logged in, the "pwd" ("print working directory") command will tell you that "/u/me" is your current directory. To list the contents of file "baz", just type

cat baz

If you want to list "foo.c" you have to supply a "path":

cat /u/me/c/foo.c         or
cat c/foo.c

The first form is an "absolute path" (starting with a "/"), the second a "relative path", starting at your current directory. Another possibility is to go to the "c" directory and then specify the filename without a path:

cd c
cat foo.c

Now your current directory is /u/me/c. To list "bar.f" from this directory without leaving it, you may use

cat ../fortran/bar.f

The ".." means to go back one level.
You may use a "~" to specify your home directory, so either of

cd ~
cd ..
cd /u/me

will bring you back there.

If you list the contents of a directory using the command

ls -la

(ls = "list", with options "long" and "all") you see something like this:

drwxr-xr-x   2 me      mygrp        512 Oct 25 18:58 ./
drwxr-xr-x 42 me mygrp 6144 Oct 26 09:53 ../
-rw------- 1 me mygrp 3395 Jul 26 1993 a.very.secret.file
drwxr-xr-x 1 me mygrp 3546 Oct 23 21:56 example/
-rw-r--r-- 1 me mygrp 1879 Oct 25 18:55 tree2
-rwxr-xr-x 1 me mygrp 1879 Oct 25 16:03 a.out*

You guess the meaning of the last fields: the size (in bytes), the date and time of the last modification and the name of the files. The third and fourth fields specify the owner ("me") and the group-owner ("mygrp").
The characters in the first field show the permission bits of the file. The first column is usually "-" (it's a regular file) or "d" (it's a directory). Next come three "rwx"-triplets specifying the access rights for the owner of the file, users of the specified group, and everyone else. "r", "w" and "x" denote the rights to Read, Write or eXecute a file; a "-" denies these rights. So for example
-rw-r--r-- lets everyone read a file, but only the owner may write to it
-rw-rw---- allows writing for users of the group, put doesn't permit reading by others
-r-------- is top secret: only the owner of the file may read it.
-rwxr-xr-x most often for applications: everyone can read and execute it, but only the owner may modify it.

Hint: if you want to execute a program and get "permission denied." Then most probably the "x"-bit is not set !

For directories the meaning of these bits is similar: You'll need write permission to create or delete a directory and execute permissions to list a directory.
You can change these permissions using the chmod command:

chmod o-r tree2         deny reading by others
chmod ugo+x tree allow execution for user, group and others

Returning to the example file listing above you'll note that the ls command appended a "/" to the name of directories and an "*" to the name of executable files. That's just for convenience; these characters don't belong to the names.
Every directory contains two special directories: "." (the directory itself) and ".." (the parent directory). You won't see these, or any other files and directories whose name starts with a dot, if you specify the ls command without the -a option. Besides that, dots in filenames have no special meaning.

On some systems you'll see directories that are "mounted" from another machines (like the "cray" directory on our file system example). For security reasons these mounts are sometimes "read-only", so you cannot write although the permission bits would allow that.
Finally you see a mount point "afs" (Andrew File System) in the example. This is a world wide distributed file system; the permission bits have little significance in this case. AFS is discussed in part two.

Previous: Unix-Command Next : Shells

Document Actions