Navigation

In this chapter, our initial focus, beyond basic typing skills, lies in mastering navigation through the file system on our Linux operating system. We’ll delve into the following commands:

  • pwd: This command prints the name of the current working directory.
  • cd: It stands for “change directory” and is used to switch between directories.
  • ls: This command lists the contents of a directory.

Understanding The File System Tree

Like Windows, a Unix-like operating system such as Linux organizes its files in what is
called a hierarchical directory structure. This means that they are organized in a tree-like
pattern of directories (sometimes called folders in other systems), which may contain
files and other directories. The first directory in the file system is called the root directory. The root directory contains files and subdirectories, which contain more files and
subdirectories and so on and so on.

Note that unlike Windows, which has a separate file system tree for each storage device,
Unix-like systems such as Linux always have a single file system tree, regardless of how
many drives or storage devices are attached to the computer. Storage devices are attached
(or more correctly, mounted) at various points on the tree according to the whims of the
system administrator, the person (or persons) responsible for the maintenance of the system.

While many of us are accustomed to navigating through a graphical file manager, the command line offers a different perspective on the file system. Instead of visual representations, we rely on conceptualizing it as a maze resembling an upside-down tree.

In this mental picture, we stand at the center of the maze, representing the current working directory. From here, we can observe the files within the directory, the pathway leading to the directory above (known as the parent directory), and any subdirectories branching below. This directory where we stand is termed the current working directory.

To unveil the current working directory in the command line interface, we utilize the “pwd” (print working directory) command. This simple command reveals our current position within the intricate file system maze.

Upon logging into our system or initiating a terminal session, our current working directory defaults to our home directory. Each user account is allocated its own home directory, serving as the designated space where regular users can create and modify files. This directory is exclusive to each user and provides a secure environment for personal file management.

Listing The Contents Of A Directory

To display the files and directories located in the current working directory, we utilize the “ls” command.

Indeed, the versatility of the “ls” command extends beyond merely listing the contents of the current working directory. It can also be employed to list the contents of any directory within the file system. Furthermore, “ls” offers various additional functionalities that we will explore further in the upcoming chapter.

Changing The Current Working Directory

To shift our current working directory within the tree-shaped maze analogy, we employ the “cd” command. To execute this, simply type “cd” followed by the desired directory’s pathname. A pathname represents the route along the branches of the tree to reach the desired directory. Pathnames can be specified in two ways: absolute pathnames or relative pathnames. We’ll first delve into absolute pathnames.

Absolute Pathnames

An absolute pathname originates from the root directory and progresses along the tree’s branches until reaching the intended directory or file. For instance, consider a directory on your system housing the majority of your system’s programs. Its absolute pathname is “/usr/bin”. This signifies that starting from the root directory (denoted by the leading slash), there exists a directory named “usr” containing another directory named “bin”.

[me@linuxbox ~]$ cd /usr/bin
[me@linuxbox bin]$ pwd
/usr/bin
[me@linuxbox bin]$ ls

Now we can see that we have changed the current working directory to /usr/bin and
that it is full of files. Notice how the shell prompt has changed? As a convenience, it is
usually set up to automatically display the name of the working directory.

Relative Pathnames

While an absolute pathname initiates from the root directory and navigates to its target, a relative pathname commences from the working directory. It employs special symbols to denote relative positions within the file system tree. These symbols are “.” (dot) and “..” (dot dot).

The “.” symbol signifies the current working directory, while “..” represents the working directory’s parent directory. To illustrate, let’s execute changing the working directory to “/usr/bin” once more:

[me@linuxbox ~]$ cd /usr/bin
[me@linuxbox bin]$ pwd
/usr/bin

Sure, let’s imagine we want to shift the working directory to the parent of “/usr/bin”, which is “/usr”. We have two methods to accomplish this task. The first approach entails using an absolute pathname:

[me@linuxbox bin]$ cd /usr
[me@linuxbox usr]$ pwd
/usr

Or, with a relative pathname:

[me@linuxbox bin]$ cd ..
[me@linuxbox usr]$ pwd
/usr

With both methods yielding identical results, the preferred choice is the one demanding the least typing effort! Similarly, transitioning the working directory from “/usr” to “/usr/bin” offers two avenues. The first involves employing an absolute pathname:

[me@linuxbox usr]$ cd /usr/bin
[me@linuxbox bin]$ pwd
/usr/bin

Or, with a relative pathname:

[me@linuxbox usr]$ cd ./bin
[me@linuxbox bin]$ pwd
/usr/bin

Now, there is something important that I must point out here. In almost all cases, you can omit the “./”. It is implied. Typing:

[me@linuxbox usr]$ cd bin

does the same thing. In general, if you do not specify a pathname to something, the working directory will be assumed.