Bash Built-in Command cd: Everything You Need to Know About Changing Directories

Learn everything about Bash's cd command—from basic directory navigation to symbolic links, logical vs physical paths, useful shortcuts, and scripting best practices. A detailed beginner-friendly guide with practical examples.

Bash Built-in Command cd: Everything You Need to Know About Changing Directories

If you've spent more than five minutes working in a Linux terminal, you've probably used the cd command hundreds of times without even thinking about it. In fact, it's one of the very first commands that every Linux user learns.

Need to navigate to another folder? Use cd. Need to return to your home directory? Use cd. Working on shell scripts or system administration tasks? You're almost certainly using cd dozens of times a day.

But despite its simplicity, the cd command is much more interesting than it might appear at first glance. It supports logical and physical paths, understands symbolic links, can take you back to your previous directory, and even updates several useful environment variables automatically.

In this article, we'll take a detailed look at how the Bash built-in command cd works, explain all of its options, and explore some practical examples that every Bash beginner should know.

What Is the cd Command?

The cd command stands for:

Change Directory

Its purpose is simple; it changes the current working directory of your shell session.

Unlike many Linux commands, cd is not an external executable program. It is a Bash built-in command.

You can verify this yourself by running:

type cd

which produces:

cd is a shell builtin

This design choice makes perfect sense. If cd were an external program, it would only change its own working directory and immediately exit, leaving your shell in exactly the same place.

Because changing the current directory affects the running shell itself, Bash implements cd internally.

Basic Syntax

The general syntax of the command is:

cd [-LP] [directory]

cd [-LP] -

Where:

  • directory is the path you want to navigate to.
  • -L tells Bash to use the logical path.
  • -P tells Bash to use the physical path.
  • - switches back to the previous working directory.

The Simplest Example

Let's start with the most common use case.

Suppose you're currently in:

/home/pavel

and want to navigate to your Documents folder.

Simply type:

cd Documents

Then check your location:

pwd

Output:

/home/pavel/Documents

Nothing surprising here.

Relative vs Absolute Paths

The cd command supports both relative and absolute paths.

Relative Paths

Relative paths are based on your current location.

For example:

cd Projects

or:

cd ../Downloads

or:

cd ./Scripts

These paths are interpreted relative to your current working directory.

Absolute Paths

Absolute paths always begin from the root directory:

/

Examples:

cd /home/pavel/Documents

cd /etc

cd /var/log

Absolute paths are particularly useful inside shell scripts because they are independent of your current location.

Checking Your Current Directory

Before moving on, let's talk about one of cd's best friends:

pwd

which stands for:

Print Working Directory

Example:

pwd

Output:

/home/pavel/Projects

You'll often use both commands together:

pwd
cd Documents
pwd

This is probably the most common navigation pattern in Linux.

Returning Home

One of the most convenient features of cd is that it understands when no arguments are provided.

Simply run:

cd

and Bash automatically takes you to your home directory.

This is equivalent to:

cd ~

For example:

cd /var/log

cd

pwd

Output:

/home/pavel

No matter where you are in the filesystem, cd without arguments will always bring you home.

Going Back to the Previous Directory

Another extremely useful feature is:

cd -

This command switches between your current and previous working directories.

Example:

cd /etc

cd /var/log

cd -

Output:

/etc

Run it again:

cd -

Output:

/var/log

This makes moving back and forth between two locations incredibly fast.

Many Linux administrators use this trick dozens of times every day.

Environment Variables Updated by cd

Every time you change directories, Bash automatically updates two important variables:

PWD

and

OLDPWD

You can view them using:

echo $PWD
echo $OLDPWD

For example:

cd /etc

echo $PWD

Output:

/etc

Then:

cd /var

echo $OLDPWD

Output:

/etc

This is precisely how:

cd -

works internally.

Understanding Logical and Physical Paths

This is where things start getting interesting.

Linux supports symbolic links (or symlinks).

A symbolic link is essentially a shortcut that points to another directory or file.

For example:

ls -ld /usr/tmp

Output:

lrwxrwxrwx 1 root root 10 Dec 26 2008 /usr/tmp -> ../var/tmp

Here:

/usr/tmp

is actually a symbolic link that points to:

/var/tmp

Now let's see what happens when we use the cd command.

Logical Paths (-L)

The logical mode preserves the symbolic link.

Example:

cd -L /usr/tmp

Then:

pwd

Output:

/usr/tmp

Even though the real directory is:

/var/tmp

Bash remembers the logical path that you provided.

This behavior is usually the default.

Logical mode is often preferable because it preserves the directory structure as the user sees it.

Physical Paths (-P)

Physical mode behaves differently.

Instead of preserving symbolic links, Bash resolves them to their actual locations.

Example:

cd -P /usr/tmp

pwd

Output:

/var/tmp

In this case, Bash follows the symbolic link and moves directly to the physical directory.

Why Does This Matter?

You may be wondering:

Why would I ever care about logical or physical paths?

For casual users, you probably won't.

However, when working with:

  • shell scripts
  • symbolic links
  • mounted directories
  • Docker environments
  • network storage
  • development tools

knowing the real physical location of a directory can become extremely important.

Imagine the following structure:

Projects
│
├── Current -> Release-3.2
│
└── Release-3.2

If your deployment script uses:

cd Projects/Current

Should Bash treat:

Current

as the actual directory name or resolve it to:

Release-3.2

Depending on your use case, both behaviors can be useful.

That's exactly why Bash provides both options.

Using cd in Shell Scripts

The cd command is extremely common inside shell scripts.

Example:

#!/bin/bash

cd /var/log

echo "Current directory:"
pwd

ls -la

A more defensive approach is often recommended:

cd /var/log || exit 1

If the directory doesn't exist, the script exits immediately.

This prevents situations where your script accidentally starts operating in the wrong directory.

For example:

#!/bin/bash

cd /important/files || exit 1

rm *

Without the error check, things can become very exciting very quickly—and not in a good way.

Fun Fact: You Can Override cd

Since cd is a Bash built-in command, you can even redefine it using shell functions.

For example:

cd()
{
    command cd "$@"

    echo "You are now in:"
    echo "$PWD"
}

Now every time you change directories, Bash automatically tells you where you've ended up.

Output:

You are now in:
/home/pavel/Documents

This can be surprisingly useful when building custom development environments.

Common cd Shortcuts

Some useful shortcuts include:

cd

Go to your home directory.

cd ~

Go to your home directory.

cd ..

Go one level up.

cd -

Return to the previous directory.

cd /

Go to the root directory.

cd ~/Downloads

Go to the Downloads directory inside your home folder.

These simple shortcuts can save a tremendous amount of typing over time.

Final Thoughts

The cd command may look deceptively simple, but it's one of the most important tools you'll ever use in Bash. Understanding how it works, including logical versus physical paths, symbolic links, environment variables, and directory navigation tricks, will make your daily work in the terminal much more efficient.

For most users, cd is simply the command used to move around the filesystem. For Bash programmers and system administrators, however, it's also an excellent example of why built-in shell commands exist and how Bash interacts with the underlying operating system.

The next time you type cd, you'll know that there's a little more happening behind the scenes than simply moving from one folder to another.

Read next