This tutorial shows how to go back to the previous or parent directories using the cd (Change Directory) command. Additionally, this content includes a complete guide to move quickly between directories in the Linux terminal.
This tutorial is optimized for inexperienced Linux users. By reading this documentation, they will fully understand how the cd (Change Directory) command works and its applications. All instructions described include screenshots, making it easy for every Linux user to understand and apply them.
The cd command implementations to move between directories in the Linux terminal explained in this article include the following cd usage examples:
The Linux Directory Structure (For New Linux Users)
Note: If you already know the basic Linux directory structure, you can jump straight to instructions to go back to previous or parent directories.
First, I want to remind new Linux users that the Linux directory structure is hierarchical. Directories within directories are subdirectories. The directories and subdirectories tree are what we call the “path”. When we specify a path, we are specifying parent directories and subdirectories.
In Linux, the main directory is what we call the root directory. It is the top directory containing the rest of the system directories. Users see a slash after their hostname when the current directory is the root directory, as shown in the following image (Violet slash).
The root directory contains system core directories, including the /boot directory, which contains boot partition and/or directories and files, and the /usr partition and/or directory, which contains program directories and files.
The following image shows the / (root) directory content, most of which is universal for all Linux distributions:
For example, in the path /etc/apt/, the root directory / (First slash) is the parent directory of the /etc subdirectory, which is the parent directory of the /apt directory.
How To Go Back to a Directory in Linux
This section explains how to go back to your previous current directory and to go back to a parent directory using the cd command in Linux.
In the following example, the linuxhint user’s first current directory is his home directory. In the first line, the user uses the cd command to move to the /etc directory, following the hierarchical path beginning from the root directory.
To go back from the current directory (/etc/) to the previous one, the user runs the cd command followed by a hyphen, as shown in the second line of the following figure:
As you can see in the previous image, the user returned to the previous directory by running cd -.
The previous example explains how to return to the previous directory the user was in.
Now, let’s say the user does not want to return to the previous directory he was in, but he wants to go back to the hierarchical tree.
Here, a user is in his home directory, and he moves to the /etc/apt subdirectory. Instead of returning to the previous directory he was in (His home directory), he wants to move to the parent directory of the current one (In this case, the parent directory of /apt is /etc). For this, the user will run the command cd followed by two dots, as shown below:
As you can see in the previous figure, the user moved to the parent directory of the previous one (/etc) and not to the previous directory he was in (/home/linuxhint).
As you can see, cd – moves the user to his previous current directory, while cd .. moves him to the current parent directory.
How To Move to the Parent Directory of the Current Parent Directory (Two Levels Up)
As said in the previous example, the cd .. command allows us to move to the parent directory. If the user wants to move to the parent directory of the parent directory (Two levels up), he needs to type the two dots twice, separated by a slash.
In the following example, the user’s current directory is linuxhint2, located under the linuxhint directory, which is located under the Desktop directory within the user home.
Let’s say the user wants to move two levels up, from the linuxhint2 directory to the Desktop directory. In this case, he only needs to use the two dots twice, separated by a slash, as shown in the following screenshot:
The previously executed command instructs Bash to take the user two directories up in the hierarchical tree. You also would implement additional dots and slashes to move to upper levels in the directory tree.
Using the TAB Key With the cd Command to Autocomplete Paths
The keyboard key tab plays an important role when using the cd command. It helps users move between directories without typing the whole path and without knowing the final path in advance.
In the following example, four subdirectories begin with the same name, but only the last parts of their names change: such as linuxhint, linuxhint2, and linuxhint3.
Suppose the user runs cd linuxhint and presses the TAB key twice. Then, the console returns all existing subdirectories, whose names begin with the path typed by the user. Thus, informing the user of the available subdirectories for him to type the complete full path.
In the following image, the user executed cd Desktop/linuxhint and then pressed the TAB key:
Even if the user does partially type the destination full path, the TAB key will also show all available subdirectories within the directory we want to access.
In the following example, the user knows he wants to access a subdirectory within his home directory. But he does not know what is the specific subdirectory he wants to access.
In this case, the user can run cd /parentdirectory/ and press the TAB key twice. This will print all subdirectories within the parent directory:
How To Move to the Home Directory in the Linux Terminal
Moving to your home directory is pretty simple. Just run the cd command without additional options, as shown below:
You also can use a tilde (~) to specify your home directory as a path. By running the cd command followed by a tilde, you will move to your home directory.
You can use the tilde to move to any user’s home directory. To accomplish it, type the username whose directory you want to move to, after the tilde symbol. As shown in the following example, the linuxhint user moves to the torvalds directory.
Move to a Directory With Spaces in its Name
Let’s assume the user wants to move to a directory whose name contains spaces to end this tutorial. In this case, the user only needs to type the directory name between quotation marks, as shown in the following screenshot.
In the following example, the user implements quotation marks to move into the Linux Hint directory:
The article discussed how the cd command allows users to move between different locations faster than the graphical user interface. All commands shown in this content are easy to learn and implement, and required for any Linux user.
The cd command is also available in other operating systems, like macOS or MSDOS, and the command is universal for all Linux distributions. I recommend readers to utilize the provided examples to make terminal browsing easier.
I hope all tips provided are useful for new users. Keep following us for more professional Linux articles.