Navigating the Shell

Chapter 02

Navigating the Shell Lecture Video

Apart from mastering the art of typing, our initial focus revolves around navigating the file system on our Linux system. In this chapter, we will familiarise ourselves with the following commands, essential for this purpose:

  • pwd - Print Working Directory

  • cd - Change Directory

  • ls - List Directory content

Grasping the File System Hierarchy

Similar to Windows, a Unix-like operating system like Linux employs a hierarchical directory structure to organise its files. This entails arranging them in a tree-like formation of directories (referred to as folders in certain systems), housing both files and additional directories. At the pinnacle of this structure lies the root directory, serving as the initial directory in the file system. The root directory encompasses files and subdirectories, which in turn harbour further files and subdirectories, perpetuating this pattern indefinitely.

It's worth noting that unlike Windows, where each storage device has its distinct file system tree, Unix-like systems like Linux maintain a unified file system tree, regardless of the number of drives or storage devices connected to the computer. Storage devices are linked (or more accurately, mounted) at different locations within the tree, based on the decisions made by the system administrator—the individual(s) responsible for system maintenance.

The Present Working Directory

Many of us are likely acquainted with a graphical file manager that visually presents the file system tree, as depicted in Figure 1. It is important to observe that the tree is commonly depicted in an inverted manner, with the root positioned at the top and the branches cascading downwards.

Nevertheless, in the command line interface, visual representations are absent, necessitating a distinct mindset for navigating the file system tree.

Figure 1: Visualisation of the file system tree depicted in a graphical file manager

Envision the file system as an inverted tree-shaped maze, where we can position ourselves at its centre. Within this structure, we reside within a particular directory at any given moment, perceiving the files contained within that directory, the pathway leading to the directory above (known as the parent directory), and any subordinate directories below. The directory in which we are currently positioned is referred to as the current working directory. To reveal the current working directory, we employ the pwd (print working directory) command.

Upon initial login to our system (or commencement of a terminal emulator session), our current working directory defaults to our designated home directory. Each user account possesses its individual home directory, which serves as the exclusive location where regular users can create and modify files.

Displaying the Contents of a Directory

To retrieve a listing of files and directories within the current working directory, we utilise the ls command.

In fact, the ls command allows us to list the contents of any directory, not limited to the current working directory, and it offers various additional functionalities to explore. We will delve deeper into the capabilities of ls in the upcoming chapter.

Modifying the Current Working Directory

Altering the current working directory (our position within the tree-shaped maze) is accomplished by employing the cd command. To achieve this, input cd followed by the desired working directory's pathname. A pathname represents the pathway we traverse along the branches of the tree to reach the intended directory. Pathnames can be specified in either of two ways: as absolute pathnames or relative pathnames. First, let's address absolute pathnames.

Absolute Pathnames

An absolute pathname commences from the root directory and proceeds through each branch of the tree until the path to the desired directory or file is accomplished. For instance, consider a directory on your system where the majority of your system's programs are installed. The pathname for this directory is /usr/bin. This implies that starting from the root directory (denoted by the leading slash in the pathname), there exists a directory named usr which, in turn, houses a directory named bin.

Observing the current status, we can perceive that the current working directory has been modified to /usr/bin, showcasing a multitude of files within. Take note of the altered shell prompt. As a convenience, it is typically configured to automatically exhibit the name of the working directory for ease of reference.

Relative Pathnames

While an absolute pathname originates from the root directory and navigates to its target, a relative pathname commences from the working directory itself. To achieve this, it employs specific symbols that indicate relative positions within the file system tree. These symbols include . (dot) and .. (dot dot).

The symbol . denotes the current working directory, while .. signifies the parent directory of the working directory. Here's a demonstration of their functionality. Let's alter the working directory back to /usr/bin:

Alright, now let's assume that we wish to modify the working directory to the parent directory of /usr/bin, which is /usr. There are two distinct approaches to achieve this. The first method involves employing an absolute pathname:

Or, with a relative pathname:

Two alternative methods that yield identical outcomes. So, which one should we opt for? The answer lies in choosing the option that necessitates minimal typing!

Similarly, we have two different approaches to modify the working directory from /usr to /usr/bin. The first method involves employing an absolute pathname:

Or, with a relative pathname:

Now, there is a crucial point that needs to be highlighted here. In nearly all cases, the ./ can be omitted as it is implicitly understood. Thus, you can simply type:

Both commands perform the same function. As a general rule, if you omit specifying a specific pathname, the working directory will be assumed by default.

Some Helpful Shortcuts

The following table displays several convenient methods to swiftly alter the current working directory.

Shortcut
Result

cd

Modifies the current working directory to your designated home directory.

cd -

Alters the current working directory to the preceding working directory.

cd ~user_name

Modifies the current working directory to the home directory of the specified user, referred to as user_name. For instance, using the command cd ~john will change the directory to the home directory of the user named john.

Important Facts About Filenames

  1. Files that commence with a period character are deemed hidden, implying that they will not be listed by the ls command unless the option ls -a is specified. When your account was created, numerous hidden files were deposited in your home directory to configure specific account settings. Later on, we will delve deeper into some of these files to explore how you can personalize your environment. Additionally, certain applications store their configuration and settings files as hidden files within your home directory.

  2. In Linux, similar to Unix, filenames and commands are case-sensitive. This implies that the filenames File1 and file1 correspond to distinct files.

  3. Linux does not have a predefined concept of a "file extension" like certain other operating systems. You have the freedom to name files as you wish. The contents and purpose of a file in Linux are determined through alternative methods. While Unix-like operating systems do not rely on file extensions to determine file contents or purpose, certain application programs may make use of them.

  4. While Linux allows for long filenames that can include spaces and various punctuation characters, it is advisable to restrict the use of punctuation characters in filenames to period (.), dash (-), and underscore (_). It is especially important to avoid embedding spaces in filenames. Instead, consider using underscore characters to represent spaces between words in a filename. Following this practice can prove beneficial in the future.

Summary

In this chapter, we explored how the shell interacts with the directory structure of the system. We gained an understanding of absolute and relative pathnames, as well as the essential commands for navigating through this structure. In the upcoming chapter, we will leverage this knowledge to take a comprehensive tour of a contemporary Linux system.

Last updated