In Linux, directories are similar to the folders used in the Windows operating system and have the same purpose. Linux, a strong command line supportive OS, offers numerous commands to manage directories. For instance, the “pwd” command refers to the present working directory. Similarly, there is a command utility, “cd”, which enables you to change the directory in the terminal to access files. This post will demonstrate the working and usage of the “cd” command in Linux. This blog will explain the below-mentioned sections::
- What is the cd Command Utility in Linux?
- What is the Usage of the cd Command in Linux?
- Bonus Tip: Most Used cd Commands in Linux
Let’s start the guide.
What is the cd Command Utility in Linux?
The cd command is abbreviated for the “Change Directory” and is used to navigate to different directories. The cd command can be used in Linux by the following syntax:
$ cd [Options] [Directory]
The options which can be used with the cd command are listed in the table below:
-L | This option is used to follow the symbolic links as well if present in the directory |
-p | This option is used to follow the physical directory structure instead of the symbolic links |
–help | This option is used to display the usage information of the cd command |
–version | This option is used to display the installed version of the cd command |
To understand more about the usage of the cd command, we will demonstrate some examples.
What is the Usage of the cd Command in Linux?
The cd command is used to navigate the different directories, and its usage is explained with the help of the examples.
Example 1: How to Change the Current Directory in Linux?
In this example, we will use the cd command to navigate to another directory. For example, the following command will switch the current working directory using the relative path (the path to reach the target directory followed by the current directory):
$ cd Downloads
We successfully change to the Downloads directory. It also allows you to change the directory using the absolute (complete) path of the target directory. As an example, the following command will navigate to the “Documents” directory using the complete path (the path of the target directory w.r.t the home directory):
$ cd /home/itslinux/Documents/
Now, we are in the “Documents” directory.
Example 2: How to Navigate to the Last Directory in Linux?
In the previous example, we changed to two different directories using the cd command. If we want to undo our selection of the cd command, then we can use the “-” with the cd command:
$ cd -
As we can see in the above output that our directory has been changed from Documents to the Downloads Directory.
Example 3: How to Navigate to the Last Directory in Linux?
To navigate from any location in the Linux to the home directory, use the “~” with the cd command described below:
$ cd ~
We are now in the home directory.
Example 4: How to Navigate to the Parent Directory in Linux?
One of the options of the cd command allows us to change our path to the parent directory by using the “..” with the cd command as follows:
$ cd ..
We are back to the parent directory of “myFolder”, which is the “Downloads” directory.
Example 5: How to Switch to the Root Directory in Linux?
To change the current working directory of the terminal to the root directory, use the “/” with the cd command:
$ cd /
To confirm that we are in the root directory, list down all the contents using the “ls” command:
$ ls
We are in the root directory.
Bonus Tip: Most Used cd Commands in Linux
For better understanding, we have added some of the most used cd commands in the following table alongside their description/purpose:
Purpose | Command |
---|---|
Switch to the Root Directory | $ cd / |
Switch to the Parent Directory | $ cd .. |
Switch to Home Directory | $ cd ~ |
Switch to the Previous Directory | $ cd – |
That’s all from this guide!
Conclusion
In Linux, the cd command is used to navigate the directory to access or view its files. The terminal’s directory can be changed to perform specific operations at that destination. This post has briefly explained the importance, working, and usage of the cd command in Linux. Furtherly, we have presented a table that describes the usage of the cd command with different purposes.
Check our LinkedIn company page