Skip to main content

Directory Operation Commands

cd Command Examples

Learn to use cd command to its full potential in Linux with these practical examples.

Command cd is used to navigate between directories in Linux. It stands for 'change directory'.

It enables you to change the working directory from the current directory to the desired directory that you wish to navigate to.

The syntax for cd command is following:

cd [option] <directory>

[option] is used to control the output of the command. You won’t be using these options most of the time.

The available options for the cd command are related to symbolic links:

  • -P: don’t follow symbolic links.
  • -L: follow symbolic links.

<directory> is where you specify the path to the desired directory that you wish to navigate to.

Before we start looking more into cd command, I suggest revisiting the concept of absolute and relative path in Linux. You'll be using them a lot with the cd command.

7 Essential examples of cd command in Linux

Here are the most common usages of cd command. Some of them you probably already know. Some of them are not so popular yet extremely useful.

💡
When typing a directory name, just hit tab after typing a few letters. It will show you all the options starting with those letters. Tab completion is a must use Linux terminal shortcut.

1. Switch to root directory

Root directory is the most important directory in the Linux file system. It is the parent directory to all other directories present in the file system. It is denoted by /. You can navigate to the root directory from any other directory by using the following command.

cd /

Output:

abhi@linux:~/parent$ cd /
abhi@linux:/$ pwd
/
abhi@linux:/$

2. Switch to child directory

A directory present inside another directory is called child directory. The directory that contains the child directory is the parent directory. You can navigate to the child directory by using the following command:

cd <child directory name>

Output:

abhi@linux:~/parent$ ls
child 'child directory'
abhi@linux:~/parent$ cd child1
abhi@linux:~/parent/child1$ pwd
/home/abhi/parent/child1
abhi@linux:~/parent/child1$

Note: When a directory name has two or more words, enclose the directory name within ” “.

abhi@linux:~/parent$ ls
child1 'child directory'
abhi@linux:~/parent$ cd "child directory"
/home/abhi/parent/child directory
abhi@linux:~/parent/child directory$

3. Use absolute pathname

Pathname beginning from the root directory (/) is called absolute pathname. You get absolute pathname by tracing the path from the root directory to the destination directory. Absolute pathname always begins from the root directory.

abhi@linux:/$ cd /home/abhi/parent
abhi@linux:~/parent$ pwd
/home/abhi/parent
abhi@linux:~/parent$

4. Use relative pathname

Pathname which begins from the current working directory is called relative pathname. You get relative pathname by tracing the path from the current working directory to the destination directory. Relative pathname always begins from the current working directory.

abhi@linux:~/parent$ ls
child1 'child directory'
abhi@linux:~/parent$ cd child1
abhi@linux:~/parent/child1$

5. Using .. to go up the directory

.. is a special link present in every directory which points to its parent directory. .. is a hidden link. To navigate to the parent directory which is one level above the child directory you can use the following command.

cd ..

Here's the output:

abhi@linux:~/parent/child directory$ pwd
/home/abhi/parent/child directory
abhi@linux:~/parent/child directory$ cd ..
abhi@linux:~/parent$ pwd
/home/abhi/parent
abhi@linux:~/parent$

You can also navigate to any higher level directories using .. required number of times. Following example shows the navigation to a two-level higher directory from the current working directory.

abhi@linux:~/parent/child1/child2$ pwd
/home/abhi/parent/child1/child2
abhi@linux:~/parent/child1/child2$ cd ../..
abhi@linux:~/parent$ pwd
/home/abhi/parent
abhi@linux:~/parent$

6. Switch back to the previous directory (very useful)

When you need to navigate back to the previous working directory from the current working directory, you can use option.

cd -

Output is:

abhi@linux:~/parent/child1/child2$ pwd
/home/abhi/parent/child1/child2
abhi@linux:~/parent/child1/child2$ cd ../..
abhi@linux:~/parent$ pwd
/home/abhi/parent
abhi@linux:~/parent$ cd -
/home/abhi/parent/child1/child2
abhi@linux:~/parent/child1/child2$ pwd
/home/abhi/parent/child1/child2
abhi@linux:~/parent/child1/child2$

7. Switch back to the home directory

~ is used to navigate back to the home directory from any other directory.

cd ~

Output is:

abhi@linux:~/parent/child1/child2$ cd ~
abhi@linux:~$ pwd
 /home/abhi
abhi@linux:~$

In fact, in many Linux distributions, you can simply type cd and enter to return to your home directory.

Conclusion

Here's a summary of the most common usage of the cd command.

Command Description
cd / Switch to the root directory (/)
cd dir_name Switch to the dir (with absolute or relative path)
cd .. To navigate to the parent directory
cd - Switch back to the previous directory
cd ~ Switch back to the home directory

I hope that you have a better understanding of these cd command examples. If you have any questions related to cd command, feel free to ask your questions in the comment section below!