If you are just getting started in Linux and its terminal capabilities, the
cd command is one of the commands you should know.
cd command refers to change directory command. As the name suggests, the command is used to move around from various directories within the filesystem.
Keep in mind that the
cd command will move from your current working directory into the specified one. Luckily, the Linux terminal will always put you in a specific directory until you change it. By default, you'll be working in your home directory.
cd command is cross-platform. This means that once you learn it here, you can use it in all Unix-based and Windows systems.
cd Command Syntax
Let us start by exploring the syntax of the
cd command. The command provides a relatively simple syntax with minimal and easy-to-follow parameters.
The command syntax is as shown:
cd [OPTIONS] directory
The command accepts two main arguments:
-L- forces the
cdcommand to follow symbolic links. This is the default function if the parameter is not specified.
-P- restricts follow of symbolic links.
The above options allow you to allow or disallow the
cd command from following symbolic links.
Keep in mind that running the
cd command without any argument will take you into your home directory. For example:
The command above shows the current working directory using the
pwd command. We can run the d
cd command without arguments as:
The command above navigates back to the home directory.
TIP: If you have auto-completion enabled on your shell, you can use tabs to autocomplete directory paths and names when working with the
Absolute and Relative Paths
When using the
cd command, you need to specify the path to the target directory. You can specify the path to the target directory as an absolute or relative path.
An absolute path refers to the path that starts at the system root's directory. This means that the path starts at
/ and follows the directory tree.
A relative path on the other hand starts from your current working directory. This is useful when you need to navigate into children directories from your current working directory.
Desktop folder in your working home directory.
In Absolute path, the path to the
Desktop directory is expressed as:
If you are in your home directory, the relative path would be expressed as:
Both commands works similarly.
The Parent Directory
In Linux and other Unix based systems, the current working directory is denoted by use of a single dot
. . Hence, running the command:
Will stay in your current working directory.
You can use dot notation to navigate back to the directories that are directly above it. For example, if you are in your
Desktop folder, you can go back to your home directory with the command:
The command above will go to the directory that is immediately above the current one. In this case, that directory is your home directory.
To move two levels up, run the command:
If you are in the
Desktop folder, this will take you into the home folder.
This can be useful when you need to quickly back into directories.
Navigate into Home Directory
As mentioned, if you run the
cd command without any arguments, the command will navigate into your home directory.
Another way to do this is using the tilde operator (
In Linux, the
~ operator means the home directory.
You can also use the tilde to navigate into subdirectories within your home folder. For example, to navigate into the
Desktop folder, run:
This basically means; cd into
If you wish to navigate into the home folder of another use, you can use the tilde operator and the username of target user.
For example, to navigate into the
debian user's home folder:
Navigate back to the Previous Directory
If you have currently navigated into another directory and you wish to go back to your previous working directory, you can use the
- operator as:
The command above will take you into your previously working directory.
debian@dev-server-0:~$ cd /var/log/
debian@dev-server-0:/var/log$ cd /usr/share/
debian@dev-server-0:/usr/share$ cd -
debian@dev-server-0:/var/log$ cd -
Note how the command allows you to switch back and forth between current working directory.
Navigating into Directories with Spaces
Unfortunately the Linux terminal and the
cd command does not like spaces. Therefore, if the directory you wish to navigate to contains spaces, you might face an error.
To fix this, simply enclose the path to the target directory with quotes.
cd '/to the path of the directory/'
Congratulations, you have successfully leveled up on your Linux command-line skills. Using this tutorial, you learned how to navigate into directories using the
If you enjoyed this tutorial, share and leave us a comment below.
Thanks for reading!!!