Purpose of cd Command
The cd (change directory) command in Linux is an essential tool for changing between files. Cd is one of the frequently used commands by the majority of Linux users when working in the terminal. All in all, it enables users to manage files in a better and more efficient way. When we launch open shell, it starts in the home directory. However, the cd command allows users to switch between directories for various tasks.
In this article, we’ll learn about the cd command by demonstrating it with examples.
Syntax of cd Command in Linux
As discussed earlier, the cd command switches or navigates between different directories. In this section, we’ll cover the basic syntax of the cd command:
$ cd directory
Here’s the breakdown of this syntax:
- cd: The command used to change the current working directory.
- directory: The location of the directory you want to move to.
Let’s head to the Downloads directory for demonstration purposes by running the following command:
$ cd Downloads
As you can notice, we’ve successfully moved from the default Home directory to the Downloads directory.
Navigating Directories with Absolute Path
Using an absolute path allows you to navigate to any directory in the Linux file system, starting from the root directory. The syntax is as follows:
$ cd /path/to/directory
For instance, we can navigate to the Downloads directory by providing its absolute path /home/muser/Downloads:
$ cd /home/muser/Downloads
Overall, navigating directories with an absolute path ensures accuracy and reliability.
Navigating Directories with Relative Path
An absolute path specifies a directory’s location starting from the root directory, providing the full path to any directory in the file system. The key difference between absolute and relative paths is that the relative path is based on your current directory and doesn’t start from the root, whereas an absolute path always starts from the root directory (/):
For example, we can run the following cd command to navigate to the Downloads directory using a relative path:
$ cd Downloads
In the above example, the cd Downloads is a relative path because it navigates to the Downloads directory based on the current working directory, not from the root. Overall, using a relative path for navigation provides a quicker way to move between directories based on your current location.
Let’s learn cd command usage for some common scenarios.
cd Command Options
Like many commands, the cd command also comes with a few flags, which we’ll now discuss below:
Navigating Back to Home Directory
In Linux, the Home directory is the default working directory. You can return to it by simply using the cd command without any flags or names:
$ cd
We were in the Downloads directory, and by running the above command, eventually, we’d return to the home directory.
Navigating to Previous Directory
To back to the last directory, use the – flag.
$ cd -
After executing the above command, we navigated to the previous directory (home) from the Downloads directory.
Navigating to Root Directory
To go to the root or top-level directory, use the cd command with /:
$ cd /
Finally, you’re now in the root directory.
Navigating to Parent Directory
By using .. with cd command, we can switch to the parent directory:
$ cd ..
Notably, after running the command, we are now in the parent directory. This way, we’ve learned the basic usage of cd command in Linux. Run the cd –help command to learn more about it.
Conclusion
All in all, the cd command in Linux is an essential tool for switching between the file system. We can use the cd command to navigate between directories by providing their absolute or relative path in the command. It’s no doubt that the cd command is one of the frequently utilized commands by Linux users.
I’m Malaikah, a Digital Forensics and Cyber Security student and CEH certified, with a passion for writing about Linux and the tech world.