cd Command in Linux

Last updated: March 15, 2022

CD is one of the very basic commands in Linux which is used very often. It is called the change directory command.

In this tutorial, we learn how to use the cd command in Linux. Here will cover its syntax and navigate directory structure with basic examples.

Prerequisites 

  • Access to Linux terminal.
  • Required permission to run the command.
  • A good smile to learn.

CD Command 

The cd command is used to change the current working directory in Linux. The word 'cd' means change directory.

The current directory is the directory in which the user is currently working. Pwd command print the path of the working directory

The syntax of the cd command:

cd [options] directory

Cd command options:

-L: Force follow symbolic links. The default is the same.

-P: Use the physical directory structure without following symlinks.

How to CD to a Directory 

To change a directory, use the cd along with specifying the directory path.

Syntax:

$ cd [path/to/directory]

Use the pwd command to print the present working directory then, use the absolute or relative path to navigate into another directory. You can use ls command to list directories and files in the file system.

For example, I am currently working in /home/linuxopsys directory, and to change into the Pictures directory, type:

$ cd Pictures
change directory

How to Use CD Command

Linux cd command allows you to change the directory using the relative and absolute paths, change to the root directory, parent directory, move into the previous directory, and more.

Change to Absolute or Relative pathname 

The absolute path starts from the system root `/` whereas the relative path starts from the current working directory. 

When you login into the Linux system, the default working directory is set to your home directory.

Let's change the directory using the relative path, here I change to Documents directory from the current working directory:

$ cd Documents
change directory using relative path

To access the same Documents directory using the absolute path, use the following command:

$ cd /home/username/Documents
change directory using absolute path

Change to Home Directory 

There are two ways to change to the Home directory, simply type cd or cd ~ from the terminal.

$ cd ~
change to home directory

For example, navigate to Documents directory which is inside Home directory, type:

$ cd ~/Documents
change to specific directory in home

To change into specific user’s home directory, use the following command:

$ cd ~username
change to specific users home directory

Make sure you have enough privilege to change to the user's home directory.

Change to Root Directory 

The root directory is the first directory or the main directory in the file system hierarchy which is represented by a single slash (`/`).

To change to the root directory, type cd command followed by / symbol:

$ cd /
change to root directory

Change directory to next level 

To change to the parent directory of the current working directory, type cd .. or cd ../ from the terminal.

change to parent directory

Basically, move one directory level up from the current directory.

You can also change into a specific directory by one level up:

$ cd ../Desktop
change to a specific directory by one level up

To move into the two-level up from the current directory, use this command:

$ cd ../../
change directory by two level up

Change to Previous working Directory

To change to the previous working directory, you can use the following command:

$ cd -
change to previous directory

In the example, we were on /home/linuxopsys directory and after running cd - command, the current working directory changed to the previous directory /home/linuxopsys/Documents.

Change to Directory with space in the name 

You may some noticed directory names with space. This commonly occurs when directories are moved from Windows.

To prevent space from being parsed, you should use quotes. For example to change a directory that has spaces in its name, type :

$ cd 'my personal directory'

You may also use a special character backslash (\) to ignore the space as follows:

$ cd my\ personal\ directory

CD with other commands

For more productivity, you can use cd command with other commands. For example, change to Documents directory and list the contents, type:

$ cd Documents && ls
cd command with ls

Conclusion 

In this tutorial, we have demonstrated how to use the Linux cd command in this tutorial.

Please feel free to provide your feedback and suggestions.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment