+(91)70149-37521Subscribe Now

cd Command in Linux: Practical Examples

The cd command in Linux changes the current working directory in your terminal. You can move to an absolute path, enter a subdirectory, return to the parent directory, switch to your home folder, or jump back to the previous location. This practical guide explains the most useful cd examples, path rules, spaces, symbolic links, shell […]

How to use cd command in linux

The cd command in Linux changes the current working directory in your terminal. You can move to an absolute path, enter a subdirectory, return to the parent directory, switch to your home folder, or jump back to the previous location.

This practical guide explains the most useful cd examples, path rules, spaces, symbolic links, shell scripts, and common errors.

What does the cd command do?

cd means “change directory.” It is a shell builtin because it must change the working directory of the current shell. An external program could change its own directory, but it could not move your active terminal into another folder.

Check your current location with:

pwd

List the files and directories there:

ls

Basic cd command syntax

cd [directory]

If the directory change succeeds, cd normally prints nothing. Run pwd to confirm the new location.

Change to an absolute path

An absolute path starts at the root directory /:

cd /var/www/html
pwd

Absolute paths work the same way regardless of your current location.

Change to a relative path

A relative path starts from the current directory. If you are in /var/www, enter its html subdirectory with:

cd html

You can move through several relative levels:

cd project/public/images

Go to your home directory

Run cd without an argument:

cd

These forms also use your home directory:

cd ~
cd "$HOME"

To enter a folder inside it:

cd ~/projects

Go to the parent directory

Two dots represent the parent:

cd ..

Move up two levels:

cd ../..

A single dot represents the current directory. cd . therefore leaves you in the same place.

Go to the root directory

cd /

The root directory is the top of the Linux filesystem. It is different from the root user’s home directory, which is normally /root.

Return to the previous directory

cd -

Bash uses the OLDPWD variable for this shortcut and prints the directory it changes to. Run cd - again to switch back.

Change to another user’s home directory

cd ~username

This works when the account exists and you have execute permission on the required directories. Do not confuse directory access with file ownership.

Handle directory names with spaces

Quote the path:

cd "Project Files"

Or escape each space:

cd Project\ Files

Quoting is usually clearer, especially in scripts. Pressing Tab after typing the first few characters can complete and escape a path automatically.

Use special characters safely

A directory beginning with a hyphen can be mistaken for an option. Add ./:

cd ./-archive

Quote paths containing shell characters such as spaces, parentheses, or wildcard symbols:

cd "Reports (2026)"

Do not use eval to build a cd command from untrusted input.

Logical and physical paths

Symbolic links can make the displayed path different from the physical filesystem path.

Logical mode is the usual behavior:

cd -L linked-directory
pwd -L

Physical mode resolves symbolic links:

cd -P linked-directory
pwd -P

This difference becomes important when using .. after entering a symbolic link. Use pwd -P when you need the real filesystem location.

Use CDPATH for frequently used directories

CDPATH is a colon-separated search path for cd. For example:

export CDPATH="$HOME/projects:$HOME/work"

You could then run cd website to find a matching folder inside those locations. Add the export to the correct shell startup file if you want it to persist.

Be careful in scripts: a user’s CDPATH can make relative changes behave unexpectedly and may print a path. Scripts can clear it locally with CDPATH= or use explicit paths.

Use cd in a shell script

Always check whether the change succeeded:

cd /var/www/example || exit 1
printf 'Working in: %s\n' "$PWD"

Without the check, later commands might run in the wrong directory. That can be dangerous for commands that modify or delete files.

To change directory temporarily, use a subshell:

(
  cd /var/www/example || exit 1
  ./run-task.sh
)

pwd

When the subshell ends, the parent shell remains in its original directory.

Use pushd and popd for several locations

Bash provides a directory stack:

pushd /var/www/example
dirs
popd

pushd saves the current directory and moves to another one. popd returns to the saved location. This is useful when switching among several project folders.

Useful cd command examples

# Home directory
cd

# Root directory
cd /

# Parent directory
cd ..

# Previous directory
cd -

# Absolute path
cd /etc/nginx

# Folder in your home directory
cd ~/projects

# Path containing spaces
cd "Project Files"

# Physical path through symbolic links
cd -P /path/to/link

Common cd command errors

No such file or directory

Check spelling, letter case, and the current location:

pwd
ls -la

Linux paths are case-sensitive, so Projects and projects can be different directories.

Permission denied

You need execute permission on each directory in the path. Inspect them with:

namei -l /path/to/directory

Do not solve the problem with broad permissions such as chmod 777. Fix the appropriate ownership, group membership, or permission instead. Our chown command guide explains ownership changes.

cd: too many arguments

The path probably contains unquoted spaces. Use cd "Directory Name".

cd works in a script but not in my terminal

A separately executed script runs in a child shell, so it cannot change the parent shell’s directory. Use a shell function, an alias for a fixed path, or source a trusted script when changing the current shell is intentional.

Frequently asked questions

How do I go back one directory in Linux?

Use cd .. to go to the parent. Use cd - to return to the directory you visited immediately before the current one.

What is the difference between / and ~?

/ is the root of the entire filesystem. ~ expands to the current user’s home directory.

Why is cd a shell builtin?

The command must modify the working directory of the running shell itself. An ordinary child process cannot change its parent’s directory.

How do I print the current directory?

Use pwd. Use pwd -P when you want symbolic links resolved to the physical location.

Official reference

The cd command in Linux becomes easy once you understand absolute paths, relative paths, .., ~, and -. In scripts, quote paths and stop immediately when a directory change fails.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to Our Newsletter

Get free how-to tutorials and over 700+ courses. Seo tips, create a wordpress, or learn a new skill.