The Wayback Machine - https://web.archive.org/web/20240711070524/https://www.geeksforgeeks.org/cd-command-in-linux-with-examples/
Open In App

How to Change the Directory in Linux | cd Command

Last Updated : 22 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
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.

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.

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.

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

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.

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.

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

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-

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

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.

Conclusion

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.



Previous Article
Next Article

Similar Reads

How to Display Current Working Directory in Linux | pwd Command
The 'pwd,' which stands for "print working directory." In this article, we will delve into the 'pwd' command, exploring its functionality, usage, and various examples. It prints the path of the working directory, starting from the root. pwd is shell built-in command(pwd) or an actual binary(/bin/pwd). $PWD is an environment variable that stores the
4 min read
mindepth and maxdepth in Linux find() command for limiting search to a specific directory.
How to limit search a specified directory in Linux? There is a command in Linux to search for files in a directory hierarchy known as 'find'. It searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known (the left-hand side i
3 min read
How to Create Directory in Linux | mkdir Command
In Linux, the 'mkdir' command is like a magic wand for creating folders super easily. 'mkdir' stands for "make directory," and it helps you organize your computer stuff by creating folders with just one command. Whether you're making one folder or a bunch of them in a row, 'mkdir' is there to help you keep things neat and tidy on your computer. In
7 min read
Shell Script to Delete a File from Every Directory Above the Present Working Directory
In this article, we will discuss how to delete a file from every directory above the parent working directory using shell script in Linux. To delete a file from a directory we use the rm command in Linux/UNIX. Syntax: rm [OPTION]... [FILE]... Using shell script, we have to delete the input file from every directory above the present working directo
2 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
How to use 'cp' command to exclude a specific directory
cp command is used to create an exact copy of the contents of the files and directories but with a different name and location. It works using the terminal. The exact syntax goes like this: Syntax:cp [source_file/directory] [destination]Where cp is used for the copy, source_file provides the exact file or directory or folder from where you want to
3 min read
bd – Quickly Go Back to a Parent Directory Instead of Typing ‘cd’ Command
IntroductionEvery Linux user most probably suffers in a situation where we need to jump from one directory to another in a backward direction, multiple times. Conventionally most of us choose to use the '..' parameter with the 'cd' command to denote the previous directory, multiple times. This command might look like this, 'cd ../../../..'. This co
6 min read
How to Change User Password in Linux | passwd Command
Securing user accounts is a fundamental aspect of maintaining a robust and secure Linux system. One essential task is changing user passwords regularly to prevent unauthorized access. The passwd command in Linux provides a straightforward and effective way to modify user passwords. This article will guide you through the process of changing user pa
7 min read
How to Change File Ownership in Linux | chown Command
In the Linux operating system, file ownership is a crucial aspect of system security and user management. The `chown` command, short for "change owner," is a powerful tool that allows users to change the owner of files and directories. This command is particularly useful in scenarios where administrators need to grant or revoke access to specific r
7 min read