June, 2010 LBSIG

Directories

Unlike Windows, where there are drive letters such as C: or D:, Unix/Linux has only a single directory tree, with the beginning point called /, or root.

Paths

a path is the location of a file or directory, using as many directory names as necessary, separated by "/", to make the file unique. There are two types:

Searching Paths

A special environment variable called PATH has a list of directories, separated by colons (:) showing where and in what order the system should look for programs to be executed. Here’s a typical value:

/home/dierdorf/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
A request for a program called "foo" will look for /home/dierdorf/bin/foo, /usr/local/sbin/foo, and so on in order.

A second special variable called CDPATH has a similar list of directories showing where the system should look for targets of the cd (change directory) command. Here’s mine:

.:/home/dierdorf:/home/dierdorf/data:/home/dierdorf/ebooks:/home/dierdorf/data/images

This is extremely useful because pathnames can be quite long. For example, I might wish to go to the directory /home/dierdorf/data/ebooks/sfnovels. With the CDPATH shown above, I can simply say cd sfnovels, no matter what my current directory is.

Both PATH and CDPATH are normally specified in the shell’s startup file: typically .bashrc or .zshrc

Mount Points

The fundamental structure of a hard drive is divided into partitions, each of which is formatted with a particular file system. Common file systems are ext3, ntfs, fat32, and so on. In Windows, each of these drive/partition combinations will be assigned a drive letter, normally beginning with C:. In Linux, however, each hardware partition is an entry in the /dev directory. Typical entries are /dev/sda1 (the first partition of the first hard drive), /dev/sdc4 (the fourth partition of the third hard drive), and so on. In order to be “visible”, these hardware designation must be mounted on (i.e., associated with) an existing directory. Typically, one might say

mount /dev/sda2 /home/dierdorf/data

The root directory (/) is always mounted automatically when the computer is booted. Linux consults a file called /etc/fstab to determine what hardware should be mounted at which directory. Here’s a typical example:

# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
proc              /proc           proc    defaults                   0 0
/dev/sda6         /               ext4    relatime,errors=remount-ro 0 1
/dev/sda5         none            swap    sw                         0 0
/dev/sda8         none            swap    sw                         0 0
/dev/scd0         /media/cdrom0   udf,iso9660 user,noauto,exec,utf8  0 0
gw:/home/dierdorf /gw             nfs     users,noauto,relatime      0 0
/dev/sda7         /extra          ext4    users,noauto,relatime      0 0

Note that /media/cdrom0, /gw, and /extra must be mounted manually by the user — the others are mounted automatically at boot time.

File System Checking

A utility called fsck can test and (hopefully) repair a file system. This MUST NOT be done if the file system is mounted somewhere — it will almost certainly destroy the data. For everything but /, it is possible to unmount the file system using the umount command and then run fsck. For example:

umount /dev/sda7
sudo fsck /dev/sda7
mount /dev/sda7 /extra

(If you’re merely curious about a mounted file system, including root, use the command

sudo fsck -n /dev/sda6  or  fsck -n /
This will report errors but make no attempt to fix them. Another useful option is -C, which shows a progress bar. It can take a long time to check a large partition!)

If you need to check your root directory, it must not be mounted, which means Linux cannot be running! This is one of the main reasons for having a System Rescue Disk handy. When the SRD is running off its CDROM, none of the hard disk partitions are in use and therefore can be checked and repaired safely. (This is also true for re-arranging a disk by adding or deleting partitions, changing their sizes, and so on. See my comments about gparted.

Using PARTED

Parted and it’s graphical version, gparted, are used to rearrange a hard disk. It can create, delete, move, and resize a given partition, as long as it is not mounted! Again, a System Rescue Disk must be used for gparted operations on the root partition of a Linux system, since that must be mounted when that Linux version is actually running.

Other Disk Utilities