Commands

7 Practical Examples of cd Command in Linux

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

Abhishek Prakash
Abhishek Prakash

Table of Contents

Command cd is used to navigate between directories in Linux. In fact, cd 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, let us recollect two other commands, pwd and ls. These commands are important when you are dealing with directories in Linux.

What is pwd command?

Command pwd in Linux is used to determine the directory that you are working in.

pwd

The output of the command for me is as shown below.

[email protected]:~/parent$ pwd
/home/abhi/parent

What is ls command?

ls command in Linux is used to list the contents of the current working directory.

ls

The output of the command is as shown below.

[email protected]:~/parent$ ls
child1 'child directory'

Now that you have seen briefly about cd, pwd and ls commands, let’s see some examples of 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.

Tip: 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:

[email protected]:~/parent$ cd /
[email protected]:/$ pwd
/
[email protected]:/$

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:

[email protected]:~/parent$ ls
child 'child directory'
[email protected]:~/parent$ cd child1
[email protected]:~/parent/child1$ pwd
/home/abhi/parent/child1
[email protected]:~/parent/child1$

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

[email protected]:~/parent$ ls
child1 'child directory'
[email protected]:~/parent$ cd "child directory"
/home/abhi/parent/child directory
[email protected]:~/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.

[email protected]:/$ cd /home/abhi/parent
[email protected]:~/parent$ pwd
/home/abhi/parent
[email protected]:~/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.

[email protected]:~/parent$ ls
child1 'child directory'
[email protected]:~/parent$ cd child1
[email protected]:~/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:

[email protected]:~/parent/child directory$ pwd
/home/abhi/parent/child directory
[email protected]:~/parent/child directory$ cd ..
[email protected]:~/parent$ pwd
/home/abhi/parent
[email protected]x:~/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.

[email protected]:~/parent/child1/child2$ pwd
/home/abhi/parent/child1/child2
[email protected]:~/parent/child1/child2$ cd ../..
[email protected]:~/parent$ pwd
/home/abhi/parent
[email protected]:~/parent$

6. Switch back to the previous directory

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

cd -

Output is:

[email protected]:~/parent/child1/child2$ pwd
/home/abhi/parent/child1/child2
[email protected]:~/parent/child1/child2$ cd ../..
[email protected]:~/parent$ pwd
/home/abhi/parent
[email protected]:~/parent$ cd -
/home/abhi/parent/child1/child2
[email protected]:~/parent/child1/child2$ pwd
/home/abhi/parent/child1/child2
[email protected]:~/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:

[email protected]:~/parent/child1/child2$ cd ~
[email protected]:~$ pwd
 /home/abhi
[email protected]:~$

In fact, in many Linux distributions, you can simply type cd and enter to return to your 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!



Join the conversation.