Some commands in Linux are so frequently used that we often overlook the significance of the commands and the details about them get missed. cd is one such command. cd stands for ‘change directory’ which itself explains its use and purpose.

cd allows you to easily change your current directory to whichever directory you wish to move. Just put the right path in the command and you will be placed in that directory by cd.

In this brief tutorial, you will get all the basic and useful insights into the cd command-line utility.

Knowing more about the cd command

cd command is a useful utility for all the frequent command-line users and also who are required to manage GUI-less servers.

Let us look at the basic syntax of the cd command.

General Syntax:

cd [options] [directory_or_directory_path]

The following table will give you a brief insight into what happens when you use these options with the cd command.

OptionSignificance
/changes the present directory to the root directory
~changes the directory to the home directory
.Represents the current directory
..change to the parent directory of the current directory
  • cd: You can change your directory directly by enetring the name of the destination directory.

General Syntax:

cd [directory_name]

Example:

cd workspace

Here, we have simply changed the current directory to a directory named ‘workspace’.

Note: Please note that this workspace directory should be placed in your present working directory. If it’s not, then you will get an error. You can use the complete path of the desired directory with the cd command. We will be learning about this in the upcoming examples.

cd / : This command will change your current directory to the root directory.

Example:

gaurav@ubuntu:~/workspace$ cd /
gaurav@ubuntu:/$

Here, we have changed the current working directory from ‘workspace’ to root directory.

gaurav@ubuntu:/$ pwd
/
gaurav@ubuntu:/$

On using the pwd (print working directory) command the ‘ / ‘ (root) directory is displayed.

  • cd ~ : This command takes you back to the home directory from whichever directory you might be working into.

Example:

gaurav@ubuntu:~/space/apache$ pwd
/home/gaurav/space/apache

I am currently in the directory named apache. Let us now use the cd ~ (tilde) command.

gaurav@ubuntu:~/space/apache$ cd ~
gaurav@ubuntu:~$ 
gaurav@ubuntu:~$ pwd
/home/gaurav
gaurav@ubuntu:~$ 

Now, we are back to the home directory ‘/home/gaurav’.

  • cd .. : This command allows you to change your current working directory to the parent directory one level above your present directory.

Example:

gaurav@ubuntu:~/snap/htop/1332$ pwd
/home/gaurav/snap/htop/1332
gaurav@ubuntu:~/snap/htop/1332$

In this example, /home/gaurav/snap/htop/1332 is the current working directory path. We are actually into the directory 1332. The immediate parent directory of the ‘1332’ directory is ‘htop’ directory. On using the cd .. command, we will move to the ‘htop’ directory, its immediate parent directory.

gaurav@ubuntu:~/snap/htop/1332$ cd ..
gaurav@ubuntu:~/snap/htop$
gaurav@ubuntu:~/snap/htop$ pwd
/home/gaurav/snap/htop
gaurav@ubuntu:~/snap/htop$

Above given are some useful options used with the cd command. Now, let us dive into some more detailed examples of the cd command.


Changing from current directory to a specific path

You can use the cd command, to change to any directory using its path.

Syntax:

cd [absolute_path_of_directory]

Example:

cd ./snap/htop/1332/examples

Here, we wish to change to a directory named ‘examples’ placed at the path /home/gaurav/snap/htop/1332/examples from the home directory.

Note: Here, I have used ./ instead of typing in the complete path of my home directory. You can learn more about it in this article.

gaurav@ubuntu:~/snap/htop1332/examples$ pwd
/home/gaurav/snap/htop/1332/examples
gaurav@ubuntu:~/snap/htop/1332/examples$

We are now placed in the directory ‘examples’.


Moving to directories with white-spaces in their name

There are many instances when we use ‘spaces’ while naming the directories. Sometimes, just using the cd command with the names of this type, doesn’t seems to work. But there’s a simple fix for this.

Putting the directory name inside single quotes or double quotes can solve the problem. You can simply use cd "directory name" or cd 'directory name'.

Syntax:

cd "directory name 22"

Example:

cd "Calibre Library"

Output:

gaurav@ubuntu:~$ cd "Calibre Library"
gaurav@ubuntu:~/Calibre Library$
trinity@ubuntu:~/Calibre Library$ pwd
/home/trinity/Calibre Library

We are now shifted to the Calibre Library directory which had white-space in its name.


Changing back to the previous directory

We previously saw the use of the cd .. command, which takes you to the parent directory of your current working directory. Here we will look at one more alternative for this.

The cd - (dash) command allows you to perform more or less the same action. You can move to the previous directory of your current working directory.

General Syntax:

cd -

Example:

gaurav@ubuntu:~/workspace/snap/vim-editor$ pwd
/home/gaurav/workspace/snap/vim-editor
gaurav@ubuntu:~/workspace/snap/vim-editor$ 

Here, I am currently working in the ‘vim-editor’ directory. Suppose a user wishes to move to the previous directory, then cd - can be helpful. Let’s see how.

trinity@ubuntu:~/workspace/snap/vim-editor$ cd -
/home/trinity/workspace/snap
trinity@ubuntu:~/workspace/snap$

Here, we have now moved to the previous directory named ‘snap’.


Conclusion

In this super simple tutorial, we learned about a very basic and friendly command cd (i.e change directory) used in all the Linux systems. We will now be able to navigate through multiple directories while working on the terminal without using the GUI. cd command will be easy to use after going through this tutorial.