How to Use the cd Command in Linux
If you are just getting started in Linux and its terminal capabilities, the cd
command is one of the commands you should know.
The 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.
In this article, you will learn the in and outs of the cd
command. This will enable you to use your Linux terminal to navigate through your filesystem with ease.
Luckily, the cd
command is cross-platform. This means that once you learn it here, you can use it in all Unix-based and Windows systems.
The 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 thecd
command 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:
$ pwd
/var/log
The command above shows the current working directory using the pwd
command. We can run the dcd
command without arguments as:
$ cd
$ pwd
/home/debian
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 cd
command.
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.
Take the Desktop
folder in your working home directory.
In Absolute path, the path to the Desktop
directory is expressed as:
cd /home/user/Desktop
If you are in your home directory, the relative path would be expressed as:
cd Downloads
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:
cd .
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:
cd ..
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:
cd ../../
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 (~
)
cd ~
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:
cd ~/Desktop
This basically means; cd into /home/user/Desktop
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:
cd ~debian
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:
cd -
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 -
/var/log
debian@dev-server-0:/var/log$ cd -
/usr/share
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/'
Boom!!
Conclusion
Congratulations, you have successfully leveled up on your Linux command-line skills. Using this tutorial, you learned how to navigate into directories using the cd
command.
If you enjoyed this tutorial, share and leave us a comment below.
Thanks for reading!!!