How to Use the CD Command in Linux

The "cd" command in Linux is simple but versatile. Explore the various ways to use it effectively.

The cd command, short for “change directory,” is one of the most fundamental and frequently used commands in Linux operating systems. Its primary purpose is to navigate between different directories within the file system. By enabling users to easily switch from one folder to another, the cd command plays a vital role in file management and system navigation. Here are several ways the cd command can be used.

General syntax for cd command:

$ cd [DIRECTORY...]
1. Go to immediate parent directory
cd ..

This command will get your from current directory to a directory before this.

Example:

$ pwd
/home/username/folder
$ cd ..
$ pwd
/home/username

In this example, the user was initially in the /home/username/folder directory and moved up to the /home/username directory by executing cd ...

2. Go to previous directory
cd -

This command will return you to the directory before the current. Think of it as the back button in your browser.

Example:

$ pwd
/home/username/folder1
$ cd /home/username/folder2
$ cd -
/home/username/folder1

In this example, the user first navigates to /home/username/folder2 and then uses cd - to return to the previous directory (/home/username/folder1), and the path to the previous directory is printed to the terminal.

3. Go to home directory
cd ~

The cd ~ command will bring you back to your home directory, regardless of which directory you are currently in.

Example:

$ pwd
/home/username/folder
$ cd ~
$ pwd
/home/username

In this example, the user was initially in the /home/username/folder directory and moved to the user’s home directory, /home/username, by executing cd ~.

4. Move two directories up from current location
cd ../../

Instead of doing cd .. twice to go up two directories before the current, you can use just cd ../../ instead.

Example:

$ pwd
/home/username/folder/subfolder
$ cd ../../
$ pwd
/home/username

In this example, the user was initially in the /home/username/folder/subfolder directory and moved up two levels to the /home/username directory by executing cd ../../.

5. Go to directory with spaces in their name
cd directory\ name\ with\ space

or

cd 'directory name with space'

To cd into directories with spaces in their name, you can either add left-backlash (\) at the back each word, or wrap the directory name with single quotes ('').

Example:

$ pwd
/home/username
$ ls
directory name with space  otherfolder
$ cd directory\ name\ with\ space
$ pwd
/home/username/directory name with space

In this example, the user was initially in the /home/username directory, then changed to the /home/username/directory name with space directory using the cd command with appropriate backslashes to escape the spaces in the directory name.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail