PQ
PQ.Hosting

Currency

The cd Command in Linux: Everything You Need to Know

Author
PQ
February 24, 2026
4 min read
92 views
The cd Command in Linux: Everything You Need to Know

cd is one of the first commands anyone encounters when logging into a server over SSH. It stands for change directory. Seems simple — give it a path, switch to it. But the command has a few tricks and shortcuts that genuinely speed up work in the terminal.

Syntax

cd [path]

Without arguments, cd takes you to the current user's home directory — equivalent to cd ~.

Basic Usage

Navigate to a specific directory

cd /etc/nginx

An absolute path — always starts with /. Works regardless of where the current directory is.

cd logs

A relative path — moves into the logs subdirectory from the current location.

Go up one level

cd ..

Two dots move to the parent directory. Can be combined:

cd ../../etc

Goes up two levels, then enters etc.

Go to the home directory

cd

or

cd ~

Both lead to /home/username for a regular user or /root for root.

The tilde also works inside a path:

cd ~/projects/myapp

Go to the previous directory

cd -

Returns to the directory you were in before the last cd. Useful when switching between two directories repeatedly — no need to retype the full path each time.

cd /var/log
cd /etc/nginx
cd -          # back to /var/log
cd -          # back to /etc/nginx

💡 cd - prints the path it switched to. This is standard behavior in bash and zsh.

Go to the root directory

cd /

Paths With Spaces

If a directory name contains spaces — use quotes or escaping:

cd "My Projects"
cd My\ Projects

Directory names with spaces are rare on servers, but worth knowing.

Useful Combinations

Check the current directory

After navigating, the current path is usually shown in the shell prompt. To print it explicitly:

pwd

List contents after switching

cd /var/log && ls -lh

&& runs the second command only if the first succeeds. If the directory does not exist — ls will not run.

Navigate with directory creation

cd does not create directories that do not exist. Use mkdir first:

mkdir -p /opt/myapp/config && cd /opt/myapp/config

The CDPATH Variable

CDPATH is an environment variable that works for cd the same way PATH works for executables. Set frequently used directories there, and navigating into their subdirectories becomes shorter.

export CDPATH=/var:/etc:/home

After this, cd nginx works like cd /etc/nginx — bash finds the match automatically. If multiple directories match — it takes the first one found.

To make CDPATH permanent, add the export to ~/.bashrc.

Navigating Through Symbolic Links

cd /var/www/html

If html is a symbolic link to another directory, cd follows it transparently. pwd shows the path through the link, not the real one.

To see the actual resolved path:

pwd -P

The -P flag resolves all symbolic links.

Common Errors

No such file or directory

bash: cd: /etc/ngnix: No such file or directory

Typo in the path or directory does not exist. Check what is nearby: ls /etc/ | grep ng.

Permission denied

bash: cd: /root: Permission denied

Not enough permissions to enter the directory. Note that sudo cd does not work — cd is a shell builtin, not an external command. Switch to root instead:

sudo -i
cd /root

Or switch user:

su - root

cd does not persist in scripts

In bash scripts, cd only changes the directory for the current process. Calling a script as a child process will not change the directory in the parent shell. This is expected behavior.

Quick Reference

Command What it does
cd /path Navigate by absolute path
cd folder Navigate by relative path
cd .. Go up one level
cd ../.. Go up two levels
cd or cd ~ Go to home directory
cd - Go to previous directory
cd / Go to root directory
cd ~/folder Go to subdirectory of home

Summary

cd is a simple command, but cd - and CDPATH noticeably speed up navigation when working with the file system regularly. On any Linux server — including VPS from the PQ.Hosting catalog — the command behaves the same way regardless of the distribution.

Share this article