The cd (change directory) command is used to change the current directory in which the user is currently working.
In Linux, this is one of the most common but important command and it will be used frequently. With the use of this command, you can easily move all over your directories of your system. You can move from your previous directory to a new one or to previous to previous directory, or anywhere you want. Now let’s understand how it works:
Change from current directory to a new directory:
In the below example, first we are giving pwd command to enter the current directory which is /home/codes. Now we are giving the ‘cd’ command to change my current directory and then mention the path of the new directory which is /home/codes/Desktop. Now as you can see in the next line, I have successfully entered my new directory Desktop which is also my current working directory.
codes@w3schools:~$ pwd /home/codes codes@w3schools:~$ cd /home/codes/Desktop codes@w3schools:~/Desktop$
- Change directory using absolute path:
While using absolute path, remember we have to mention the complete path starting from the root.
codes@w3schools:/run/cups$ cd /run/cups/certs codes@w3schools:/run/cups/certs$
In above example, we wanted to change our directory from ‘cups’ to ‘certs’. So what we are doing is we are providing the whole path which is /run/cups/certs starting from the root (/). This is absolute path.
- Change directory using relative path:
This below example is same as the above one, but the difference is we are providing relative path here. Here, we changed our directory from ‘cups’ to ‘certs’ but we have not mentioned the whole path but directly used the directory name which we want to enter. This is the relative path.
codes@w3schools:/run/cups$ cd certs codes@w3schools:/run/cups/certs$
|cd ~||This command brings you to your home directory.|
|cd –||It takes you to the previous directory of the current directory.|
|cd ..||It takes you to the parent directory of current directory.|
|cd /||This command will bring you to the entire system’s root directory.|
|cd../../dir1/dir2||It will take you two directories up and then move to dir1 and then finally enters dir2.|