Linux Commands

cd Command in Linux (Change Directory)

Cd Command in Linux Change Directory

The cd (Change Directory) is one of the popular and most used commands while working on the Linux Command Line. It is used to navigate and change from the present working directory to any other directory. This article will demonstrate the usage of the cd command with examples in Linux/Ubuntu 22.04 LTS. This article will discuss:

  • CD Command Syntax.
  • How to Change/Access a Sub-Directory?
  • How to Change/Access a Directory from a Directory?
  • How to Change Directory and List its Contents?
  • How to Change Directory to a Root Directory?
  • How to Change Back to Home Directory?
  • How to Change Back to the Previous Directory?
  • How to Access the Parent Directory?
  • How to Access a Directory with its Name Having Spaces?
  • How to Auto-Complete a Directory?

In the following sections, the syntax of the cd command is discussed followed by a description and examples of each used case.

CD Command Syntax

The syntax of the cd command is:

$ cd <Flags/Options> <directory>

Where,

  • cd: invokes cd command.
  • <Flags/Options>: Adding Flags/Options.
  • <directory>: Name of directory or path of directory.

How to Change/Access a Sub-Directory?

To move inside a subdirectory, the subdirectory name is followed by the cd command:

$ cd Documents

From the above image, it can be observed that the present working directory is changed to “Documents” from “Home”. Alternatively, the directory can be navigated via an absolute path as follows:

$ cd /home/linuxuser/Documents/Directory1/Directory2

From the above image, it can be observed that the present working directory is changed to “Directory2” from “Home”.

How to Access a Specific Directory from the Current Directory?

To navigate from the current directory to a specific directory, the cd command can be used:

$ cd Documents/Directory1/Directory2/Directory3

From the above image, it can be observed that the present working directory is changed to “Directory3” from “Home”.

How to Change Directory and List its Contents?

The cd command can be used alongside the list (ls) command to change the directory and list contents.

The below example demonstrates the usage of the combination of cd and list commands with a subdirectory and absolute path:

$ cd Documents && ls

or

$ cd /home/linuxuser && ls

In the first example, it can be observed that the present working directory is changed to “Documents” from “Home” while in the second example, the present working directory is changed to “Home” from “Documents”.

How to Change Directory to a Root Directory?

To change the directory to the root directory, the cd command is followed by the slash (/) symbol:

$ cd /

From the above image, it can be observed that the present working directory is changed to “root” from “Directory1”.

How to Change Back to Home Directory?

To change the directory back to the home directory from any directory, the cd command is followed by the tilde(~) symbol:

$ cd ~

Alternatively, the present working directory can be changed to the Home directory by the cd command without any options as shown below:

$ cd

From the above image, it can be observed that the present working directory is changed to the Home directory.

How to Change Back to the Previous Directory?

Changing to the previous directory from any directory demands the cd command to be followed by the dash(-) symbol:

$ cd -

From the above image, it can be observed that the present working directory is changed to “Directory1” from “root”.

How to Access the Parent Directory?

To access the parent directory from the current directory, i.e., one level up from the current directory, double period (..) symbols follow the cd command:

$ cd ..

From the above image, it can be observed that the present working directory is changed to the parent directory, i.e., from “Directory1” to “Documents”.

How to Access a Directory with its name having Spaces?

Although it’s not common, if a directory contains spaces in its name, it can be accessed by enclosing the directory name with either double(“”) or single quotes(‘’). Alternatively, by escaping every space by using a backslash (\) in the directory name:

$ cd 'My Directory 1'

Or

$ cd "My Directory 2"

Or

$ cd My\ Directory\ 3

From the above image, it can be observed that the directories with spaces in their names are accessed using the cd command.

How to Auto-Complete a Directory?

The Linux/Ubuntu terminal provides an auto-complete feature, which assists a user in case a user forgets the name of the directory or does not know the name of the directory. The Auto-Complete feature can be utilized by pressing the “Tab” button while typing the name of the directory to get auto-complete suggestions:

$ cd Doc[Tab]

Conclusion

The cd command is mainly used to navigate and change the present working directory. The syntax of the cd command is: “cd <Flags/Options> <directory>”. Using the cd command, a user can access a directory via absolute and relative paths as well as change the directory to root, home, previous, or parent directories. This article demonstrated the usage of the cd command with detailed examples in Linux/Ubuntu 22.04 LTS.

Similar Posts