Understanding how to navigate through Linux directories using the command terminal is an important skill for a system administrator, developer or cloud engineer.
In this article, we will guide you step-by-step how to use the cd command to browse through Linux directories:
What is the cd command in Linux
The cd command stands for ‘change directory’ and it is a command used to change the current directory to another. Let’s say your current directory is folder_A, and to move to a different directory, Folder_B, you would need to use the cd command on the Linux terminal.
The basic syntax for using the cd command to navigate to directories is as follows’;
cd [directory]
Navigating to an Absolute path
An absolute path is any directory path that starts from root with a forward slash. For example:
- /home/user/documents/file.txt
- /etc/nginx/nginx.conf
With the cd command, you can move a specific directory by specifying its absolute path:
cd /home/user/Documents
The above command takes you to the Documents directory.
Navigating to Relative Path
A relative path specifies the location of a file or directory in relation to the current working directory. A relative path does not start from the the root directory but starts from the current directory you are in.
For example, the following command takes us to Downloads, which is located in the current working directory:
cd Downloads
Basically, the starting point of the relative path is your current working directory you’re operating in.
RECOMMENDED READING: How to list files and directories using the ls command in Linux
Moving to the Home Directory
A home directory is the default directory allocated to every Linux user. If you want to go back to your home directory from wherever you’re on Linux, there two ways to do it:
- Typing cd without any arguments:
cd
- Using the
~
(tilde) symbol:
cd ~
Navigating Up the Directory Tree
You can move up one level in the directory hierarchy using the double dots ..
cd ..
If you need to move up multiple levels, you can repeat ..
with slashes. For example, to move up two levels:
cd ../..
Returning to the Previous Directory
To go back to the last or previous directory you in, you can use dash -
(dash):
cd -
For example, if you were in /home/user/Documents
, moved to /var/log
, and then ran cd -
, you’d return to /home/user/Documents
.
Exploring Hidden Directories
In Linux, directories that start with a dot in their names are hidden. To navigate to such directories, you need to include the dot in the path:
cd ~/.config
Navigating Through Multiple Levels in One Command
You can navigate through multiple directory levels in one command. For instance, if you are in your home directory and want to move to /home/user/Documents/work
, you can do so in one step:
cd Documents/work
Dealing with Spaces in Directory Names
If your directory names contain spaces, you need to enclose them in quotes or escape the spaces with a backslash:
cd "My Documents"
or
cd My\ Documents