We started with the basics of the home, working, and parent directories. These directories form the backbone of how we navigate and organize data in a computer system. But how do we move between and reference these directories? This is where the concepts of relative and absolute paths come into play. These paths are the methods we use to locate files and directories within the file system.

Absolute paths

An absolute path is the complete path to a file or directory from the root of the filesystem. It specifies the exact location, starting from the root directory (the root directory is the top-most directory in the file system hierarchy. The home directory is a subdirectory of the root directory).

Characteristics of absolute paths

Begins with the root directory: On Unix-like systems, an absolute path starts with a forward slash (/). On Windows systems, it starts with a drive letter (e.g., C:\).

Unambiguous and independent of the current working directory: An absolute path always points to the same location in the file system, regardless of the current working directory.

Often used in scripts or situations where a fixed file location is necessary: Because absolute paths are unambiguous, they are reliable for scripts, configuration files, and when specifying locations for system-level operations.

For example, on a Unix-like system, the absolute path to a user’s Documents directory might be /home/username/Documents.

Relative paths

A relative path points to a file or directory in relation to the current working directory. It’s more flexible as it’s based on your current location in the file system.

Characteristics of relative paths

Does not start with the root directory: A relative path omits the leading slash (/ on Unix-like systems) or drive letter (on Windows).

Changes based on the current directory: The interpretation of a relative path depends on the current working directory. If you move to a different directory, the same relative path may point to a different location.

Useful for shortening the command inputs and when working within a specific directory hierarchy: Relative paths are convenient for navigating within a project directory or when writing scripts that operate relative to the current directory.

For example, if your current working directory is /home/username, the relative path to the Documents directory would simply be Documents.

Quiz Question

Choose the statement that correctly differentiates between absolute and relative paths.

From paths to commands

After exploring the concepts of relative and absolute paths, it becomes clear how we reference and navigate through the file system in a computing environment. These paths are integral to using various command line instructions effectively. Among the most fundamental and frequently used commands in this context are cd, ls, and pwd. Let’s dive into each of these commands and uncover their functionalities and applications.

Change directories

cd stands for “change directory”. This command is used to change the current working directory in the terminal.

General usage

cd [directory]: Moves to the specified directory

cd: Without any arguments, it takes you to your home directory

cd ~: Also takes you to your home directory

cd ..: Moves you up one level to the parent directory

cd -: Takes you to the last working directory

Listing directories

ls stands for “list”. This command lists contents in the current directory.

General usage

ls: Lists contents in the current directory

ls [directory]: Lists contents in the specified directory

ls -l: Lists contents in a long format, showring permissions, owner, size, and modification date

ls -a: Lists all contents, including hidden files (those starting with .)

ls -la: Combines the features of -l and -a

Print working directory

pwd stands for “print working directory”. This command displays the current working directory’s absolute path, and it has only one usage, pwd. It is essential for confirming your current location in the file system, especially useful when scripts or programs require a precise directory path.

Quiz Question

Match the following directory shortcuts to their respective descriptions.

To recap…

The cd, ls, and pwd commands are the bread and butter of terminal navigation and file management. They work hand in hand with the concepts of relative and absolute paths, allowing you to seamlessly move through and interact with the file system. Mastery of these commands is a foundational skill for anyone working in a Unix-like command line environment, forming the basis for more advanced operations and scripting.