Javatpoint Logo
Javatpoint Logo

Linux cd Command | Linux change directory

Linux cd command is used to change the current working directory ( i.e., in which the current user is working). The "cd" stands for 'change directory.' It is one of the most frequently used commands in the Linux terminal.

Syntax:

It is one of the most important and common commands in the Linux system and will be used repeatedly. With the help of this command, we can move all over our directories in our system. We can go to our previous directory or previous to the next directory, or anywhere.

As we are familiar with Linux directories, so, we will perform the following cd operations on directories:

  1. Change from the current directory to a new directory
  2. Change directory using an absolute path
  3. Change directory using the relative path
  4. Change to the home directory
  5. Change to the previous directory
  6. Change to Parent Directory
  7. Change to the root directory
  8. Change to another user's home directory
  9. Change to Directory having Spaces
  10. Change up to multiple Sub Directories

1) Change from the current directory to a new directory

We can change our directory from the current working directory to a specified directory. To display the current working directory, execute the command as follows:

To change our current working directory, execute the command as follows:

Consider the below output:

CD Command

From the above output, we have executed the pwd command to display the current working directory, which is "/home/sssit." Then, we have executed the 'cd' command to change our current directory and mentioned the path for the new directory as "/home/sssit/Desktop." As we can see in the output image, we are on our new directory that is Desktop. Therefore, our current working directory has changed to Desktop.

2) Change directory using an absolute path

To change the directory by using an absolute path, we have to mention the whole path starting from the root. Consider the below example:

CD Command2

From the above output, we are changing our directory to 'certs' from 'cups.' So, we have provided the whole path "/run/cups/certs" starting from the root (/). This is called an absolute path.

3) Change directory using a relative path

We can change our directory by using a relative path; a relative path is a location that is relative to the current directory. Consider the below example:


CD Command

From the above output, we are changing the directory by using a relative path. Like the above example, here too, we have changed our directory from 'cups' to 'certs,' but have not mentioned the whole path. This is the relative path.

4) Change to the home directory

To change the directory to home directory from the current working directory, execute the command as follows:

The above command will bring us to our home directory. Consider the below output:

CD Command

As we can see from the above output, we were on the Downloads directory, and the "cd ~" command has brought us to our home directory.

5) Change to the previous directory

To change to the previous directory from the current working directory, execute the command as follows:

Consider the below output:

CD Command

As we can see from the above output, we were on the "/Downloads/akash" directory. And, by executing the "cd -" command, our current working directory has been changed to its previous directory, i.e."/Downloads."

6) Change to Parent Directory

To change the directory to parent directory of the current working directory, execute the command as follows:

The above command will bring us to the parent directory of the current working directory. Consider the below output:

CD Command

As we can see from the above output, the directory 'akash' has been changed to its parent directory 'Downloads.'

7) Change to the root directory

To navigate directory to entire system's root directory from current working directory, execute the command as follows:

Consider the below output:

CD Command

From the above output, the current working directory has changed to the system's root directory.

8) Change to another user's home directory

We can change the directory from the current working directory to a user's home directory by executing the command as follows:

Consider the below output:

CD Command

From the above output, we have changed the directory to the home directory of the user 'javatpoint'.

9) Change to Directory having Spaces

To change the directory that has spaces in its name, surround the path with quotes (' ') or use the backslash (\) character with it. Execute the command as follows:

The above commands will ignore the space from the directory name. Consider the below output:

CD Command

10) Change up to multiple Sub Directories

We can change directory up to multiple sub-directories by separating directories by a forward slash (/) as follows:

Consider the below output:

CD Command




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA