1. Overview
In Linux, the cd command is fundamental for navigating the filesystem. It enables us to move between directories easily from the terminal.
In this tutorial, we discuss the cd command and its various options by providing hands-on examples.
2. Understanding cd
cd stands for change directory, and its main objective is to change the current working directory:
$ cd [path_to_directory]
2.1. Absolute and Relative Paths
A path specifies the location of a directory within the filesystem. It can be absolute or relative.
An absolute path defines the exact location of a directory from the root (/) of the filesystem:
$ cd /home/francis/Desktop
With this command, we move directly to the Desktop directory, irrespective of the current location.
On the other hand, a relative path defines a directory’s location in relation to the directory we’re currently working in:
$ cd frontend
We use this approach to navigate to subdirectories. Additionally, we can navigate to a subdirectory with spaces in the name:
$ cd "personal projects"
Here, we enclose the directory name in quotes since it contains space.
It’s important to verify the current location during navigation:
$ pwd
/home/francis/Desktop/personal projects
Above, we use the pwd command to show the absolute path of the directory we’re currently working in.
Importantly, cd throws an error if the specified directory doesn’t exist.
2.2. Shortcuts
Linux provides shortcuts for navigating to specific directories. So, let’s begin with navigating to the parent directory:
$ cd ..
This command moves us up one level in the directory hierarchy from the current location. What’s more, we can use this shortcut to navigate from the current directory to a sibling directory:
$ cd ../Projects
In this example, we move up to the parent directory and then down to the sibling directory Projects.
Next, let’s explore moving to the home directory:
$ cd ~
Above, this command takes us to the home directory from anywhere in the filesystem. Alternatively, typing only cd in the terminal works the same:
$ cd
Here, we use cd without arguments to change to the home directory.
Also, let’s navigate to the root directory:
$ cd /
Here, we move to the topmost level of the filesystem.
Further, let’s change to the previous current working directory:
$ cd -
cd – instructs the shell to change to the directory we were in before we changed to the current one. It helps when we want to efficiently switch between two directories without typing the full path each time.
2.3. Symbolic Links
By default, when we use cd, it follows symbolic links, which point to another directory’s location.
To demonstrate, let’s work with the documents symbolic link that points to /home/francis/data/work. First, let’s discuss the -L option, which instructs cd to follow the symbolic link:
$ cd -L documents
Above, cd follows the documents symbolic link and takes us to the directory /home/francis/data/work.
Next, let’s utilize the -P option to instruct cd not to follow the symbolic link:
$ cd -P documents
Here, -P instructs cd to ignore the symbolic link documents. To explain, this command takes us to the directory where documents are located rather than following the link.
2.4. Using Tab Completion
When using cd, tab completion in the shell lets us quickly specify the directory we want to navigate to. First, we begin by typing a few letters of the directory name:
$ cd Pro[TAB]
For instance, if we want to change to the directory Programs, we can type cd Pro and press the Tab key. Here, Linux automatically completes the directory name Programs.
However, this happens since only one directory matches the few characters that we typed. If there are multiple matches, we need to press Tab again:
$ cd Pro
Programs/ Projects/
As displayed above, pressing the Tab key again displays a list of possible completions.
Now, we can add more characters to be more specific:
$ cd Progr[TAB]
Once we update the cd input, we can press the Tab key again to complete the directory name Programs based on this new, narrowed-down input.
3. Conclusion
In this article, we dive into how the cd command is fundamental for easily navigating the filesystem.
Understanding its various options and syntax allows us to manage files and directories skillfully within the Linux environment. Additionally, we provide examples to enforce our understanding of the cd command.