cd Command in Linux



Change directory, also referred to as cd is a Linux command that allows users to quickly navigate between the directories directly from the terminal. It is a versatile command that is widely used in Linux systems to switch between different directories. This tutorial will cover all about cd commands on Linux systems.

Table of Contents

Installation of cd Command in Linux

The cd command is a part of coreutils package that is preinstalled on most Linux distributions. However, if you have accidentally deleted the coreutils package from the system, you can reinstall it from your Linux package manager.

For Linux distributions like Ubuntu, Debian, and Linux Mint, you can use apt package manager to install the coreutils package on your system −

sudo apt install coreutils 

The other Linux distributions like REHL, CentOS and Fedora can use any of the following commands to install coreutils package on their systems −

sudo yum install coreutils 

Or,

sudo dnf install coreutils 

Syntax for cd Command in Linux

The syntax for cd command on Linux is quite simple, which is given below −

cd [directory_name/directory_path]

Different Options Available for cd Command

By default, cd command is used without an argument/option. However, you can use the below-given options from the table to change the behavior of the cd command on Linux.

Option Description
-L Follow symbolic links (default behavior). This means if you cd into a symlink, it resolves to the target directory.
-P Do not follow symbolic links. If you cd into a symlink, you remain in the symlinked directory.
-e If the directory does not exist, display an error message.
@ Display the symbolic link information (if applicable).

Examples of cd Command in Linux

Lets explore some examples of cd command, which are given below −

Switch to Root Directory

One of the basic functions of cd command is to quickly switch to a root directory (the top-level directory in the filesystem hierarchy). For example, lets switch to the /tmp directory using the following command −

cd /tmp
Switch to Root Directory

Switch to a Child Directory

To navigate to a subdirectory (child directory) within your current location, you can use the below-given command −

cd [directory_name]

For example, to switch to the directory Documents, the following command will be used −

cd Documents
Switch to a Child Directory

Note − Ensure the directory you want to switch will be there at the current location.

Switch to a Directory Using Absolute Pathname

You can provide the full path to your desired directory when using the cd command in Linux. This allows you to navigate directly to a specific location. For example, to move to a directory Documents using the absolute path, you can use the following command −

cd /home/ubuntu/Documents
Switch to a Directory Using Absolute Pathname

Go Up One Directory Level

To go up one directory level that is moving to the parent directory from the current location, you can use the following command −

cd ..
Go Up One Directory Level

Return to the Previous Directory

To return to the previous directory from the current location on Linux, you can use the below-given cd command −

cd -
Return to the Previous Directory

Return to the Home Directory

If you want to quickly return to home directory from any location, you can use the following cd command −

cd ~
Return to the Home Directory

Switch to Another User Directory

With the cd command, you can also switch to another users directory. However, this can only be possible if you are login as a root user. For example, to switch to another user linux, the following command is used −

cd ~linux
Switch to Another User Directory

Switch to a Directory with Spaces in the Name

When dealing with a directory that has spaces in its name, you can use the backslash symbol (\) to escape the spaces. For example, If the directory name is "My Documents" you would navigate to it like this −

cd My\ Documents
Switch to a Directory with Spaces in the Name

Thats how you can use the cd command on Linux to switch to directories according to your choice.

Conclusion

cd command is a versatile command used in all Linux distros to switch to a specific directory directly from the terminal. The syntax for cd command is simple and can be used with options like -L, -P, -e and @.

In this tutorial, we have explored multiple examples of cd commands starting with switching to root directory to ending with switching to a directory with space in the name. You can try all these examples to understand the basics and start navigating to a specific directory according to your choice.

Advertisements