It is nearly impossible to use Linux without knowing about the cd command. It is one of the first commands a person learns when they start working on Linux. The cd command lets us change directories. cd is short for ‘Change Directory‘.
Linux file system consists of directories and subdirectories (directories within directories). A directory can have a file or another directory inside it. While working on Linux, you need to jump from one parent directory to a sub-directory and back from subdirectory to the parent directory. All this is done using the cd command.
Let’s get started.
Using the cd command to change directory
To change your current working directory you just need to mention the name of the directory along with cd.
$ cd [directory-name]
This will take you into the mentioned directory as shown.
Whenever you use the directory name like this, you are mentioning the local pathname that is relative to the current location. Alternately you can also mention the absolute path along with cd.
An absolute pathname is in reference to the root, whereas a local pathname is in reference to the current location.
You can use the pwd command to print the current working directory.
Using cd with an absolute pathname
Using cd with an absolute pathname will take you to the mentioned directory from wherever you are in the system.
$ cd [absolute_pathname]
For example, to switch to the root directory from anywhere in the system use the command :
$ cd /
/ is the absolute pathname of root.
Using cd with absolute path name to enter a directory:
$ cd /root/test_directory
Switch to the home directory
cd can also take you directly to the home directory. The home directory is also called the login directory. It is the directory that a user is first in after logging into the system. To switch to home, enter the command below:
$ cd ~
That is cd command with the tilde (~) character.
Home directory for me is /root, therefore I am taken to /root. Alternatively you can also do this by using cd and typing the full name of your home directory.
$ cd [path_to_home_directory]
Switch to the parent directory
To switch to the parent directory of the current directory use the command :
$ cd ..
That is cd followed by two dots.
/root was the parent directory for our current working directory.
Switch to the previous working directory
You can use cd to switch to the previous working directory. Note that this is different from the switching to the parent directory. When you use an absolute pathname to arrive at the current directory, parent directory and previous directory can be different.
The command to switch to previous working directory is:
$ cd -
In this case, you can see that the parent directory and previous working directory are different since we used absolute pathname to enter the current working directory.
That brings u to the end of this basic guide to the cd command. We saw how we can use cd command to jump in and out of directories. To know more about the command you can refer to its man page.