A Command You Rarely Think About
You blink all day, every day, but, most of the time, you’re unaware of it. Unless something gets in your eye, you rarely think about that little, regular movement. Some Linux commands are like that. They hover on the periphery of your consciousness. Even though you use them daily, they don’t catch your attention because they’re so small and simple.
Within the first hour of using a Linux computer, you learn how to use the cd command included with Bash and other shells. Perhaps you had prior experience using it on another operating system and didn’t need an explanation. It changes the current working directory, right? What else is there to know?
Well, more than you’d think. Here are a few hints and tips that might improve your efficiency.
The Standard cd Operations
For the sake of being complete, let’s quickly run through the standard uses of cd.
If we’re in the home directory, but want to change to one located at /usr/lib/firefox/browser, and then return to the home directory, we can use the following commands:
You don’t have to type the whole directory path; you can use auto-complete. For each part of a path, after you type enough letters to distinguish the name of the directory from the others, press Tab to auto-complete the directory name.
For example, type the following on the command line:
Now, press Tab and the shell will fill in the rest of the “firefox” directory for you. If you add “/b” to the path and press Tab again, it adds the “browser” directory to the command.
The shell adds a trailing forward slash so you can repeat the tab-completion process. That’s also why there’s a trailing forward slash on the first command. There isn’t one on the second because that one was typed.
You can use the tilde (~) as a shorthand way to quickly return to the home directory from anywhere in the filesystem; just type the following:
cd ~
These are examples of absolute paths, in which you provide the entire path from the root of the filesystem to the target directory, to cd.
Relative paths are referenced from the current working directory. In the home directory, there’s a directory called work . You can use the tree command to see the directory tree inside the work directory—just type the following:
The work directory contains a directory called dev . There’s also a directory called dev in the root directory of the filesystem. You can use ls with -d (directory) to look at each of these. The -hl (human-readable, long listing) option tells ls to use easy to read units for the directory sizes, and the long format listing.
If you type dev, the shell assumes you mean the “dev” in the current directory. To force it to look at the “dev” in the root directory, just add a leading forward slash to represent the root of the filesystem, as shown below:
The cd command behaves like ls in this respect. If you reference the directory as dev, as shown below, it assumes you mean the directory in the work directory:
Without a leading forward slash, longer paths are assumed to start from the current working directory, too, as shown below:
RELATED: 15 Special Characters You Need to Know for Bash
Changing the Directory with Double Dot
The double dot identifier represents the parent directory of the current working one. If you’re in a deeply nested subdirectory, you can use .. with cd to move to the parent directory of the one you’re in.
This moves you up two directories in the directory tree. If you add more .. onto the command, it allows you to move an arbitrary number of levels up the directory tree.
Type the following:
You can also create a set of aliases to perform these maneuvers for you, by typing the following:
You can use these in the same way as the commands themselves.
To make the aliases consistent across reboots of your computer, you must add them to your .bashrc or .bash_aliases file.
RELATED: How to Create Aliases and Shell Functions on Linux
Easily Hop Between Two Directories
The hyphen (-) is another symbol that has a special function. It changes your directory back to the one you just came from.
For this example, let’s say you’re in the “c” directory. You can use cd to change to the “forth” directory. Then, you can use cd - to bounce back and forth between the two directories.
To do this, you type the following:
cd ../forth
cd –
cd –
The name of the directory you’re moving to appears before you move into it.
RELATED: How to Use pushd and popd on Linux
Another Kind of Relative
The shell uses the current working directory as the “root” or base directory for relative paths. You can use the CDPATH environment variable to set another location as the base directory for relative paths. If you spend most of your time in a certain section of the filesystem tree, this can save you a lot of keystrokes (and time) every day.
Let’s type the following to make work/dev/projects the base directory for relative paths:
Now, each time you use the dc command, the location in the CDPATH environment variable is checked first for matching directory names. If any of them match the target you provided in the cd command, you’re transferred to that directory.
Now, regardless of where you are in the filesystem, when you use the cd command, the shell checks whether the target directory is located in the base directory. If it is, you’re moved to that target directory.
If your target directory starts with a leading forward slash (/), which makes it an absolute path, it won’t be affected by the CDPATH environment variable.
To demonstrate this, we type the following:
The CDPATH environment variable is truly a path, just like the PATH environment variable. When you type a command, the shell searches the locations in the PATH for a match. When you use CDPATH, the shell searches the locations in the CDPATH environment variable for a match. Also, the same as PATH, CDPATH can contain multiple locations.
RELATED: How to Work with Variables in Bash
To have the shell search the current directory before other locations in the CDPATH environment variable, you just add a period ( . ) at the beginning of the path like so:
To make your settings permanent, you have to add them to a configuration file, such as .bashrc.
One thing to be aware of: If you set a base directory, it also affects directory changes performed within scripts. To avoid this, you can use absolute paths in your scripts or a test in your .bashrc file when you specify your CDPATH, as shown below:
This performs a test to see whether the command-line prompt variable, $PS1 , was set. The CDPATH environment variable will only be set if the test succeeds.
RELATED: How to Add a Directory to Your $PATH in Linux
Using shopt with cd
With the shopt command, you can set certain options for the shell. Some of these can enhance your use of cd. To set them, you use the -s (enable) option with shopt to pass an option name to it.
The cdspell option checks your directory names and corrects some common typing mistakes, including transposed or missing characters, or names with too many characters. If it finds a directory that matches any of the corrections, the corrected path is printed, and the cd action takes place.
As an example, we type the following to set the cdspell option and misspell “Desktop” to see if the shell corrects it for us:
The shell caught the error, corrected it, and changed to the “Desktop” directory.
Another shopt option you can use with cd is autocd. It eliminates the need for you to type cd at all. Anything you type that isn’t a command, script, or other executable (such as an alias), is used as a target directory. If you can transfer to that directory, it’s printed in the terminal window, and you’re changed to that directory.
As an example, we type the following:
See! You can hop all over the filesystem without even using cd!
The settings you change with shopt only affect interactive shells, not scripts.
The cd Collection
You probably won’t adopt all of these. However, it’s likely you found something of interest or benefit here. After all, anything that speeds up or simplifies your command-line navigation is all good!