If you use Linux regularly—whether on a server, a development machine, or a desktop environment—there’s one command you type more than almost any other: cd. Short for “change directory,” this simple command is the backbone of file system navigation in Linux.

Yet most users only scratch the surface of what cd can do.

In this in-depth guide, you’ll learn how the cd command in Linux works, how to use it efficiently with absolute and relative paths, how to handle symbolic links, and how to troubleshoot common errors. By the end, you’ll move through directories faster and more confidently than ever.

What Is the cd Command in Linux?

Unlike many Linux commands that are standalone programs, cd is a shell built-in command. This means it runs directly inside your current shell session (such as Bash, Zsh, or Fish).

READ 👉  Recraft AI: The Ultimate AI-Powered Graphic Design Tool for Stunning Results

Why does that matter?

Because only the shell itself can change its current working directory. External programs cannot modify the parent shell’s directory, which is why cd must be built into the shell.

Every major Linux shell includes cd by default:

  • Bash
  • Zsh
  • Fish

There is nothing to install—it’s always available.

To confirm it’s a built-in command, run:

type cd

You’ll see output indicating that cd is a shell builtin.

cd Command Syntax Explained

The general syntax of the cd command is:

cd [options] [directory]

Both the options and the directory argument are optional.

If you run:

cd

You’ll automatically return to your home directory.

Absolute vs. Relative Paths

The directory argument can be:

  • Absolute path – Starts from / (root of the filesystem)
  • Relative path – Starts from your current directory

Examples:

cd /var/log        # Absolute path
cd projects/app    # Relative path

Essential cd Command Shortcuts

Here are the most useful navigation patterns you should memorize:

TaskCommandResult
Go to home directorycd or cd ~Returns to your home
Go to previous directorycd -Switches to last location
Move up one levelcd ..Parent directory
Move up two levelscd ../..Two levels up
Go to rootcd /Filesystem root
Go to specific pathcd /etc/nginxExact directory
Handle spacescd "My Folder"Supports spaces
Another user’s homecd ~usernameThat user’s home

Let’s break these down.

How to Navigate Directories with cd

1. Move Into a Specific Directory

If a directory exists inside your current location:

cd Documents && pwd

The pwd command confirms your new location.

READ 👉  How to Use SCP in Git Bash to Transfer Files from Linux to Windows

2. Change Directory and Immediately List Files

You can combine cd with ls using &&:

cd /var/log && ls

The second command only runs if the first succeeds.

3. Use Absolute Paths

Absolute paths always start at the root (/):

cd /etc/nginx && pwd

This works regardless of where you currently are.

4. Use Relative Paths

Relative paths depend on your current directory:

cd projects/webapp && pwd

This is faster when working nearby in the directory tree.

Switching Back with cd –

One of the most powerful shortcuts is:

cd -

It instantly switches back to the previous directory.

Example:

cd /var/log
cd /etc
cd -

This toggles between two directories—perfect for checking logs while editing configuration files.

cd - uses the OLDPWD environment variable to track the last location.

Move Up the Directory Tree

Go Up One Level

cd ..

Go Up Multiple Levels

cd ../..

Each .. moves up one directory.

Return to the Home Directory

There are two equivalent ways:

cd

or

cd ~

You can also combine ~ with subfolders:

cd ~/Documents

The tilde (~) expands to your home directory using the HOME variable.

Access Another User’s Home Directory

To navigate to another user’s home:

cd ~www-data

Replace www-data with the actual username.

Note: You must have appropriate permissions.

Handling Directory Names with Spaces

Linux treats spaces as argument separators. To enter directories with spaces, use:

Double Quotes

cd "My Projects"

Or Escape Spaces

cd My\ Projects

Both methods work.

Jump Directly into Nested Subdirectories

You can navigate multiple levels in one command:

cd /var/log/apt

To create and enter a directory in one line:

mkdir -p ~/projects/newapp && cd ~/projects/newapp

Use Tab Autocomplete for Faster Navigation

Start typing a directory name and press Tab.

READ 👉  Rymdport: Securely Share Your Files with Another Wormhole Client

If there’s one match, it auto-completes.
If there are multiple matches, press Tab twice to see options.

Example:

cd /etc/n<Tab><Tab>

Tab completion prevents typos and dramatically speeds up Linux directory navigation.

Advanced cd Command Techniques

Follow or Resolve Symbolic Links (-L vs -P)

By default, cd follows symbolic links logically (-L behavior).

To force physical resolution:

cd -P /tmp/logs
  • -L → Shows the symlink path
  • -P → Shows the real filesystem path

This matters in scripts or backup operations.

Use CDPATH for Directory Shortcuts

CDPATH allows you to define search paths for frequently used directories.

Example:

export CDPATH=".:$HOME/projects:$HOME/Documents"

Now you can type:

cd webapp

Even if you’re not inside projects, the shell will search defined paths.

To make it permanent, add it to:

  • ~/.bashrc
  • ~/.zshrc

Always include . at the beginning to prioritize the current directory.

Combine cd with pushd and popd

While cd tracks only the previous directory, pushd and popd maintain a directory stack.

Example:

pushd /etc/nginx
# make changes
popd

popd returns you to the previous directory automatically.

Use this when navigating multiple locations in sequence.

Troubleshooting Common cd Errors

1. “No such file or directory”

Usually a typo:

cd /var/logs

Correct path:

cd /var/log

2. “Permission denied”

You lack execute permission:

cd /root

Check permissions:

ls -ld /root

Use sudo if authorized.

3. “Not a directory”

You tried to enter a file:

cd /etc/hostname

Verify type:

file /etc/hostname

4. cd Inside Scripts Doesn’t Change Your Terminal

If you run:

bash script.sh

The directory change won’t persist.

Instead, use:

source script.sh

This runs the script in the current shell session.

Frequently Asked Questions

What does cd stand for?

It stands for change directory.

Is cd the same in Bash, Zsh, and Fish?

Yes. Core behavior is standardized across major shells.

How do I check my current directory?

Run:

pwd

Final Thoughts: Why Mastering cd Makes You More Efficient

The cd command may seem simple, but it is one of the most powerful tools in Linux. Mastering directory navigation—using shortcuts like cd -, cd .., CDPATH, and tab completion—can dramatically improve your workflow.

Whether you’re managing servers, developing applications, or working with large file structures, efficient navigation saves time and reduces errors.

Learn these patterns once, and you’ll use them every day.

Did you enjoy this article? Feel free to share it on social media and subscribe to our newsletter so you never miss a post!

And if you'd like to go a step further in supporting us, you can treat us to a virtual coffee ☕️. Thank you for your support ❤️!
Buy Me a Coffee

Categorized in: