Bash cd Command – Navigate File System

Ever feel like you are lost in a digital maze, searching for that one important file on your Linux system? Just like you navigate physical spaces, you need a way to move through your computer’s file system. In the Linux terminal, your most important navigation tool is the simple yet mighty cd command.

The cd command, short for “change directory,” is your fundamental tool for moving around the Linux/Unix file system. Think of your file system like a city with different streets (directories) and each street with many houses (subdirectories). The cd command is how you move between these ‘locations’.

This post is for anyone new to Linux, or those who want to solidify their understanding of basic terminal navigation.

What is cd?

cd is the abbreviation for “change directory.” Its core function is to alter the shell’s current working directory. Where you are in the file system dictates what files you can easily access and what commands you can run directly.

Without cd, interacting with different parts of your system would be incredibly cumbersome and inefficient. It is important for managing files, accessing programs, and performing operations in specific locations.

Understanding Your Current Location (Working Directory)

Your Current Working Directory (CWD) is the directory you are currently “inside” in the terminal – think of it as your current ‘room’ in the file system ‘house’.

To verify your current location, you will use pwd (print working directory), the indispensable companion to cd.

pwd

Typical Output:

/home/username

Basic Usage of cd

Let us explore the fundamental ways to use the cd command.

Changing to a Specific Directory (Absolute Path)

An absolute path starts from the root directory (/) and defines the full, unambiguous path to a location, regardless of where you currently are.

cd /var/log

This command takes you directly to the /var/log directory. You could also jump to /usr/local/bin from anywhere by using its absolute path:

cd /usr/local/bin

Changing to a Specific Directory (Relative Path)

Relative paths are defined in relation to your current working directory. They are shorter but only work from specific starting points.

If you are currently in /home/user, you can navigate to a subdirectory named documents like this:

cd documents

This would change your current directory to /home/user/documents.

Going to the Home Directory

Both of these commands will take you directly to your user’s home directory (e.g., /home/your_username).

cd

Or, using the tilde (~) shortcut:

cd ~

The ~ (tilde) is a powerful, universally understood shortcut for your home directory.

Going Back to the Previous Directory

This is a powerful command for quickly toggling between the current directory and the immediately previous directory you were in. It’s extremely handy for switching back and forth!

cd -

Navigating with . and ..

These special directory references are crucial for efficient navigation.

. (Current Directory)

While cd . does not change your directory, the single dot (.) explicitly refers to the current directory.

cd .

It is often used in other commands (e.g., ./my_script.sh to execute a script in the current directory) to specify the current location.

.. (Parent Directory)

The double dot (..) moves you one level up in the directory hierarchy.

cd ..

If you are in /home/user/documents, cd .. would take you to /home/user. You can chain them to move up multiple levels:

cd ../../

This command would move you two levels up (e.g., from /home/user/documents to /home).

Practical cd Scenarios and Examples

Let us put the cd command into action with some real-world examples.

Scenario 1: Moving into a Subdirectory

You are in /home/john and want to go into your projects folder.

pwd
# Output: /home/john

cd projects
pwd
# Output: /home/john/projects

Scenario 2: Moving Up to the Parent Directory

You are in /home/john/projects and want to go back to /home/john.

pwd
# Output: /home/john/projects

cd ..
pwd
# Output: /home/john

Scenario 3: Moving to a Sibling Directory

You are in /home/john/documents and want to go to /home/john/pictures.

pwd
# Output: /home/john/documents

cd ../pictures
pwd
# Output: /home/john/pictures

First, .. moves you up to /home/john, then pictures moves you down into the pictures directory.

Scenario 4: Handling Directory Names with Spaces

Linux directory names can contain spaces, but you need to tell the shell to treat them as part of the name, not as separators between commands or arguments.

You have a directory named My Documents or Work Files.

Method 1: Using Quotes

Enclosing the entire directory name in double quotes tells the shell to treat it as a single unit, despite the space.

cd "My Documents"

Method 2: Escaping Spaces

The backslash (\) acts as an escape character, telling the shell to treat the following space as a literal character, not a separator.

cd My\ Documents

Scenario 5: Changing to a Deeply Nested Directory (Relative Path)

You are in your home directory (~) and want to reach ~/documents/reports/2025/q3.

cd documents/reports/2025/q3

You can chain directory names together with / for quick relative navigation.

Scenario 6: Changing to a Directory from Anywhere (Absolute Path)

No matter where you are, you need to quickly access /var/log/apache2.

cd /var/log/apache2

Absolute paths are your reliable way to jump directly to any known location without worrying about your current position.

Scenario 7: Using cd with ls for Discovery

Often, you do not know the exact directory name or what subdirectories are available. This workflow is very common.

Workflow:

  1. Use ls to see available directories in your current location.Bashls # Output: documents downloads projects public_html
  2. Then, use cd with the desired directory name you saw in the ls output.Bashcd projects
  3. Use ls again to see the contents of the new directory.Bashls # Output: my_app website research

Remember, ls is your ‘eye’ to help you choose your cd destination.

Scenario 8: Quick Navigation to User-Specific Application Data

You want to modify configuration files for an application like nvim (Neovim). These are often located in hidden directories.

cd ~/.config/nvim

This command uses the ~ shortcut and navigates into a hidden directory (.config). Remember, hidden directories start with a dot (.)!

Common Pitfalls and Tips

Even simple commands have nuances. Keep these tips in mind.

Case Sensitivity

Crucially, Linux file and directory names are case-sensitive. cd Documents is different from cd documents. Always double-check your capitalization!

Tab Completion

This is your best friend! Strongly recommend using the Tab key for auto-completing directory names. It saves time and prevents typos.

  • Type the first few letters (e.g., cd Doc) and press Tab. If unique, it completes the name (cd Documents/).
  • If multiple options exist (e.g., Doc could be Documents or Dockers), press Tab twice to see all possibilities.

Permissions (Brief Mention)

While cd itself generally does not fail due to permissions (unless the directory does not exist), you need read and execute permissions to list the contents of a directory (which is what ls does).

“No such file or directory” Errors

If you get this error after a cd command, it means the path you provided does not exist or you made a typo. Double-check your spelling and case!

FAQ’s – Linux/Unix Bash Command – cd


What is the cd command in Bash?
The cd (change directory) command is used in Bash to navigate between directories in a Unix/Linux file system. It changes the current working directory to the specified path.


How do I change to another directory using cd?
Use the cd command followed by the path:

cd /path/to/directory

Example:

cd /home/user/Documents

How do I go back to the previous directory using cd?
Use a hyphen (-) to return to the last directory:

cd -

This toggles between the current and previous directories.


How do I go to the home directory using cd?
You can return to the home directory with:

cd

or

cd ~

This will take you to your user’s home folder (e.g., /home/username).


What does cd .. do?
The .. represents the parent directory. So:

cd ..

moves you up one level in the directory hierarchy.


How do I move up multiple levels with cd?
You can chain .. with slashes:

cd ../../

This moves you up two levels.


How do I change into a directory with spaces in its name?
Use quotes or escape the space:

cd "My Documents"
# or
cd My\ Documents

How do I check the current directory after using cd?
Use the pwd (print working directory) command:

pwd

This shows the full path to your current location.


How can I use cd with relative paths?
Relative paths are based on your current location:

cd subfolder   # Go into a subfolder
cd ../sibling  # Go to a sibling folder

Use pwd to confirm where you are.


Why do I get “No such file or directory” when using cd?
This error usually means:

  • The path is misspelled
  • The directory does not exist
  • You do not have permission

Check the path with ls or ls -l.


How do I list all directories available to use with cd?
You can list directories using:

ls -d */

This lists all directories in the current folder.


Can I use tab completion with cd?
Yes. Bash supports tab completion. Start typing the directory name and press Tab to auto-complete or list suggestions:

cd Doc<Tab>   # may complete to Documents/

How do I set a shortcut to a frequently used directory?
Use a shell alias or environment variable.

Example with a variable:

export proj=~/projects/myapp
cd $proj

Add this to your ~/.bashrc to make it persistent.


How do I use cd with environment variables like $HOME?
You can use $HOME to refer to your home directory:

cd $HOME

This is the same as cd ~.


Can I use cd with symbolic links?
Yes. cd works with symbolic links as if they were regular directories:

cd /path/to/symlink

Use pwd -P to show the physical path, resolving symlinks.


How do I return to the root directory using cd?
Use a forward slash:

cd /

This takes you to the root of the file system.


How do I go to a user’s home directory using cd?
Use the tilde ~ followed by the username:

cd ~username

For example:

cd ~john

Goes to /home/john (if you have permission).


How do I make cd show the directory I am moving to?
Wrap it in a function:

cd() {
  builtin cd "$@" && pwd
}

Add this to your ~/.bashrc to make cd print the new directory after changing.


Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment