Filesystem Navigation - Shell Scripting

As you can see from the shell prompt, when you start a shell session you are usually placed in your home directory. Most often, you will want to break out of your home directory and want to explore other areas in the Linux system. This section describes how to do that using command line commands. Before we do that though, we should take a tour of just what the Linux fileystem looks like so we know where we’re going.

The Linux filesystem

If you’re new to the Linux system, you may be confused by how it references files and directories, especially if you’re used to the way that the Microsoft Windows operating system does that. Before exploring the Linux system, it helps to have an understanding of how it’s laid out.

The first difference you’ll notice is that Linux does not use drive letters in pathnames. In the Windows world, the physical drives installed on the PC determine the pathname of the file. Windows assigns a letter to each physical disk drive, and each drive contains its own directory structure for accessing files stored on it.

For example, in Windows you may be used to seeing the file paths such as:

c:Documents and SettingsRichMy Documents est.doc.

This indicates that the file test.doc is located in the directory My Documents, which itself is located in the directory rich. The rich directory is contained under the directory Documents and Settings, which is located on the hard disk partition assigned the letter C (usually the first hard drive on the PC).

The Windows file path tells you exactly which physical disk partition contains the file named test.doc. If you wanted to save a file on a floppy disk, you would click the icon for the A drive, which automatically uses the file path a: est.doc. This path indicates that the file is located at the root of the drive assigned the letter A, which is usually the PC’s floppy disk drive.

This is not the method used by Linux. Linux stores files within a single directory structure, called a virtual directory. The virtual directory contains file paths from all the storage devices installed on the PC, merged into a single directory structure.

The Linux virtual directory structure contains a single base directory, called the root. Directories and files beneath the root directory are listed based on the directory path used to get to them, similar to the way Windows does it.

For example, the Linux file path /home/rich/Documents/test.doc only indicates that the file test.doc is in the directory Documents, under the directory rich, which is contained in the directory home. It doesn’t provide any information as to which physical disk on the PC the file is stored on.

The tricky part about the Linux virtual directory is how it incorporates each storage device. The first hard drive installed in a Linux PC is called the root drive. The root drive contains the core of the virtual directory. Everything else builds from there.

On the root drive, Linux creates special directories called mount points. Mount points are directories in the virtual directory where you assign additional storage devices.

The virtual directory causes files and directories to appear within these mount point directories, even though they are physically stored on a different drive.
Often the system files are physically stored on the root drive, while user files are stored on a different drive, as shown in Figure below.
The Linux filesystem

In Figure above, there are two hard drives on the PC. One hard drive is associated with the root of the virtual directory (indicated by a single forward slash). Other hard drives can be mounted anywhere in the virtual directory structure. In this example, the second hard drive is mounted at the location /home, which is where the user directories are located.

The Linux filesystem structure has evolved from the Unix file structure. Unfortunately, the Unix file structure has been somewhat convoluted over the years by different flavors of Unix. Nowadays it seems that no two Unix or Linux systems follow the same filesystem structure. However, there are a few common directory names that are used for common functions. Table 4 lists some of the more common Linux virtual directory names.

When you start a new shell prompt your session starts in your home directory, which is a unique directory assigned to your user account. When you create a user account, the system normally assigns a unique directory for the account.

In the Windows world, you’re probably used to moving around the directory structure using a graphical interface. To move around the virtual directory from a CLI prompt, you’ll need to learn to use the cd command.

Traversing directories

The change directory command (cd) is what you’ll use to move your shell session to another directory in the Linux filesystem. The format of the cd command is pretty simplistic:cd destination The cd command may take a single parameter, destination, which specifies the directory name you want to go to. If you don’t specify a destination on the cd command, it will take you to your home directory.

The destination parameter, though, can be expressed using two different methods:

  • An absolute filepath
  • A relative filepath

Common linux directory names

The following sections describe the differences between these two methods.

Absolute filepaths

You can reference a directory name within the virtual directory using an absolute filepath. The absolute filepath defines exactly where the directory is in the virtual directory structure, starting at the root of the virtual directory. Sort of like a full name for a directory.

Thus, to reference the apache directory, that’s contained within the lib directory, which in turn is contained within the usr directory, you would use the absolute filepath:


With the absolute filepath there’s no doubt as to exactly where you want to go. To move to a specific location in the filesystem using the absolute filepath, you just specify the full pathname in the cd command:
rich@1[~]$cd /etc

The prompt shows that the new directory for the shell after the cd command is now /etc. You can move to any level within the entire Linux virtual directory structure using the absolute filepath:

rich@1[~]$ cd /usr/lib/apache

However, if you’re just working within your own home directory structure, often using absolute filepaths can get tedious. For example, if you’re already in the directory /home/rich, it seems somewhat cumbersome to have to type the command:

cd /home/rich/Documents

just to get to your Documents directory. Fortunately, there’s a simpler solution.

Relative filepaths

Relative filepaths allow you to specify a destination filepath relative to your current location, without having to start at the root. A relative filepath doesn’t start with a forward slash, indicating the root directory.

Instead, a relative filepath starts with either a directory name (if you’re traversing to a directory under your current directory), or a special character indicating a relative location to your current directory location. The two special characters used for this are

  • The dot (.) to represent the current directory
  • The double dot (..) to represent the parent directory

The double dot character is extremely handy when trying to traverse a directory hierarchy. For example, if you are in the Documents directory under your home directory and need to go to your Desktop directory, also under your home directory, you can do this:

rich@1[Documents]$ cd ../Desktop

The double dot character takes you back up one level to your home directory, then the /Desktop portion then takes you back down into the Desktop directory. You can use as many double dot characters as necessary to move around. For example, if you are in your home directory (/home/rich) and want to go to the /etc directory, you could type:

rich@1[~]$ cd ../../etc

Of course, in a case like this, you actually have to do more typing to use the relative filepath rather than just typing the absolute filepath, /etc!

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Shell Scripting Topics