Open In App
Related Articles

cd Command in Linux with Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

cd command in Linux known as the change directory command. It is used to move efficiently from the current working directory to different directories in our System.

Syntax of the `cd` command in Linux

cd [directory]

Here, replace [directory] with the path of the destination directory you want to navigate to.

Practical Implementation of the `cd` command with Examples.

How to move inside a subdirectory in Linux

To move inside a subdirectory in Linux we use `cd` command.

Syntax:

cd [directory_name] 

Here, replace [directory_name] with the desired directory you want to move in.

For Example:

If we want to move to a subdirectory name “Documents”

cd Documents
cd Documents

cd Documents

Here we have used,

`ls` = To display all the files and directories in the current location (directory)

`pwd` = to check the current location path or we can say the current directory name

Using `/` as an argument in `cd` command in Linux

By using `/` as an argument in `cd` we can change the directory to the root directory. The root directory is the first directory in your filesystem hierarchy. 

Syntax:

cd /
cd /

cd /

Above, / represents the root directory. and used `pwd` to check the current location path or we can say current directory name.

How to move inside a directory from a directory

This command is used to move inside a directory from a directory. 

cd dir_1/dir_2/dir_3

Here, replace “dir_1/dir_2/dir_3” with your sub directory name or location you want to move in.

For Example:

We are in “/home/raghvendra” directory and we want to move in its sub directory location (path) “Documents/geeksforgeeks/example”

cd Documents/geeksforgeeks/example
cd Documents/geeksforgeeks/example

cd Documents/geeksforgeeks/example

In the above example, we have the document directory and inside the document directory we have a directory named geeksforgeeks and inside that directory, we have an example directory. To navigate the example directory, we have used command cd Documents/geeksforgeeks/example.

How to move in home directory from any location in Linux System

`~` this argument is used in `cd` command to change the directory to the home directory from any location in Linux System.

Syntax:

cd ~

For Example: We are in location “/home/raghvendra/Documents/geeksforgeeks/example” and want to move to the home directory. We can use the following command.

cd ~
cd ~

cd ~

We can also pass `cd` command with no arguments, which will eventually land us into our home directory.

cd 
cd

cd 

How to move to parent or one level up from current directory in Linux

We use `..` this as an argument in `cd` command which is used to move to the parent directory of current directory, or the directory one level up from the current directory. “..” represents parent directory.

Syntax: 

cd .. 

For Example: We are in location “/home/raghvendra/Documents/geeksforgeeks/example” and want to move to parent or one level up in directory. We can use the following command.

cd ..
cd ..

cd ..

Use of `cd “dir name”` command in Linux 

This command is used to navigate to a directory with white spaces. Instead of using double quotes we can use single quotes then also this command will work. 

cd "dir name"

Here, replace “dir name” with the directory name you want. 

For Example: If we want to move to “dir name” = “My songs”. We use the following command.

cd "My songs"
cd "My songs"

cd “My songs”

We can also use `\` this in between if we don’t want to use double or single quotes.

cd My\ songs
cd My\ songs

cd My\ songs

Conclusion

In this, we have discussed the `cd` command in Linux which is a powerful tool that is used by users to navigate through directories efficiently. We have discussed syntax and different examples with the practical implementation of the `cd` command in Linux. Overall, we can say that one can easily understand the `cd` command and its functionality after understanding this article. It is important to learn the `cd` command as it is an essential skill for effectively navigating in Linux command-line.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 31 Jul, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials