Open In App

How to Change the Directory in Linux | cd Command

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The Linux Command Line or Linux Terminal is a super useful tool that lets people talk to the computer. One big part of using this Linux Terminal Tool is moving around the computer’s folders and files, which we call the file system. CD Command in Linux is the most important aspect of doing this.

It helps you go from one folder to another. All of your necessary tasks related to Linux File Management can be handled using the Linux CD Command. To become a Linux OS Expert, it is necessary to grab all the operations on CD Linux Command. Using CD Command in Terminal helps to change directories in Linux or move around your computer.

In this article, we’ll talk about Linux CD Command in detail along with an explanation of how it works with lots of examples.

Quick Preview to Change Directory in Linux Using CD Command:

  1. To move inside the subdirectory, use the command cd [directory]
  2. For moving inside the Root, the command cd / will be used.
  3. For moving from one directory to another directory, use the command cd dir_1/dir_2/dir_3.
  4. To directly come inside the home directory, the command cd ~ will be used.
  5. To go inside the Parent Directory, the cd .. command will be used.

Quick Highlights on Linux Terminal CD Command:

  • CD Command stands for the full-form Change Directory.
  • The CD Command is considered one of the most used Linux Commands globally.
  • The CD Command is the only source to change your directory or folder from the Linux Terminal.
  • The CD Command can take an argument or folder name to directly move inside there.
  • The CD . The command is used to know about the current directory.
  • The CD – Command is used to back to your last working directory.

What Is the Syntax of the CD Command in Linux?

The ‘cd’ command allows users to change their current working directory within the file system. The basic syntax of the `cd` command is as follows:

cd [directory]

Here, replace [directory] with the destination directory path you want to navigate to. If no directory is specified, ‘cd’ will take you to your home directory by default. Let’s explore the command’s functionality through examples.

Examples of Change the Directory in Linux Using CD Command

Now, it is time to understand the Useage of CD Command in Terminal of Linux OS. Here, we are going to discuss a few examples with practical use to clarify the CD Command Concept.

Example 1: Move Inside a Subdirectory in Linux Using CD Command

To move inside a subdirectory in Linux we use the CD Linux Command. Here, replace [directory_name] with the desired directory you want to move in.

cd [directory_name]

For Example:

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

cd Documents

1--CD-Documents

Explanation: Here, we have used the following commands:

  • `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

Example 2: Using `/` As an Argument in the 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. 

cd /

2--CD-Slash

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

Example 3: Move Inside a Directory From a Directory

This command is used to move inside a directory from a directory. Here, replace “dir_1/dir_2/dir_3” with the subdirectory name or location you want to move in.

cd dir_1/dir_2/dir_3

For Example:

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

cd Documents/geeksforgeeks/example

3--CD-Document-Geeks

Explanation: 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 the command cd Documents/geeksforgeeks/example.

Example 4: Change Directory to Home Directory From Any Location in Linux

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

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 ~

4--CD-Tilda

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

cd

5--Only-CD

Example 5: Move to Parent or One Level Up from the Current Directory in Linux

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

cd .. 

For Example:

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

cd ..

6--CD-

Example 6: Change Directory by Using 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 also work. Here, replace “dir name” with the directory name you want. 

cd "dir name"

For Example:

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

cd "My songs"

7--CD-My-Song

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

cd My\ songs

8--CD-Slash-Song

In this, we have discussed the Linux Change Directory which is super important for Changing Directories in Linux. Whether you’re going back to your Linux Home Directory, navigating through different folders, or dealing with spaces in folder names, this guide covers it all. The Command CD Linux is a key player on Linux, making it easier to get things done on the computer.

FAQs to Change Directory in Linux Using CD Command

How do I go back to the home directory using the ‘cd’ command in Linux?

To go back to the home directory, you can simply type `cd` without any arguments. This command will take you to your home directory, no matter where you are in the file system.

What is the difference between absolute and relative paths when using the ‘cd’ command in Linux?

Absolute paths start from the root directory (e.g., `/home/user/documents`), while relative paths are about your current location (e.g., ..`/pictures` to move up one level and then into the “pictures” directory). Understanding these distinctions is crucial for effective directory navigation.

How can I use the ‘cd’ command to move to a directory with spaces in its name?

If a directory has spaces in its name, enclose the entire path in quotes.

For example: To move to a directory named “My Documents,” you would type cd “My Documents”

Is there a shortcut to switch between the current and previous directories using the ‘cd’ command?

Yes, you can use the `cd -` command to quickly switch between the current and previous directories. This is handy for toggling back and forth between two locations.

Can I use the ‘cd’ command to navigate to a directory without typing the full path in Linux?

Yes, you can make use of tab completion. Start typing the directory name and press the ‘Tab’ key. The system will either complete the name if it’s unique or show you options if there are multiple matches. This makes navigating through the file system faster and more efficient.



Last Updated : 30 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads