Terminal GuideTerminal Guide

cd Command Guide

The cd (change directory) command is fundamental for navigating the Linux filesystem. Learn how to move between directories efficiently.

4 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

cd /path/to/dirChange to directory
cdGo to home directory
pwdShow current path

Shortcuts

cd ~Home directory
cd -Previous directory
cd ..Parent directory

Paths

cd /var/logAbsolute path
cd ./subdirRelative path
cd ../siblingSibling directory

Tips

cd "My Dir"Path with spaces
pushd /pathPush to dir stack
popdPop from dir stack

Downloadable Image Preview

Failed to generate preview

Basic Usage

The cd command changes the current working directory. Simply provide the path to the directory you want to navigate to.

bash
cd /path/to/directory

Common Shortcuts

Linux provides several shortcuts for common navigation patterns.

Home directory (~)

bash
# Go to home directory
cd ~

# Or simply
cd

Previous directory (-)

bash
# Go back to the previous directory
cd -

This is useful for toggling between two directories.

Parent directory (..)

bash
# Go up one directory
cd ..

# Go up two directories
cd ../..

Current directory (.)

bash
# Stay in current directory (rarely used alone)
cd .

Navigation Shortcuts

~Home directory
-Previous directory
..Parent directory
.Current directory
/Root directory

Absolute vs Relative Paths

Absolute paths

Absolute paths start from the root directory (/).

bash
cd /home/user/documents
cd /var/log
cd /etc/nginx

Relative paths

Relative paths start from the current directory.

bash
# If you're in /home/user
cd documents        # Goes to /home/user/documents
cd ./documents      # Same as above
cd ../otheruser     # Goes to /home/otheruser
Tip
Use pwd (print working directory) to see your current location at any time.

Practical Examples

bash
cd ~/projects/my-app
bash
# Type partial name and press Tab to complete
cd Doc<Tab>  # Completes to Documents
bash
# Use quotes or escape the space
cd "My Documents"
cd My\ Documents

Using environment variables

bash
cd $HOME
cd $PROJECT_DIR
Warning
If cd fails, it usually means the directory doesn't exist or you don't have permission to access it. Use ls to verify the directory exists.

Tips and Tricks

Use pushd and popd for directory stack

bash
# Push current directory and change to new one
pushd /var/log

# Pop back to previous directory
popd

Create and navigate in one command

bash
mkdir -p new-project && cd new-project

Summary

The cd command is essential for filesystem navigation. Key takeaways:

  • Use cd ~ or just cd to go home
  • Use cd - to toggle between directories
  • Use cd .. to go up one level
  • Use Tab completion for faster navigation
  • Quote paths with spaces or use backslash escaping

Related Articles