cd - Moving Around

cd stands for Change Directory. It moves you from one directory to another.

Basic Usage

Terminal
$pwd
/home/user
$cd Documents
$pwd
/home/user/Documents

After cd, you're in a new location. All relative commands now operate from there.

Going Home

Several ways to get to your home directory:

Terminal
$cd
(goes to home)
$cd ~
(goes to home)
$cd $HOME
(goes to home)

Just cd with no arguments takes you home. It's the fastest way.

Lost? Go Home

When you're lost in the file system, cd (no arguments) takes you home. Then you can start fresh.

Going Up

Use .. to move up one level:

Terminal
$pwd
/home/user/projects/website
$cd ..
$pwd
/home/user/projects

Chain them to go up multiple levels:

Terminal
$pwd
/home/user/projects/website/src
$cd ../../..
$pwd
/home/user

Going to Previous Directory

The - shortcut takes you to your previous location:

Terminal
$pwd
/home/user
$cd /var/log
$pwd
/var/log
$cd -
/home/user
$cd -
/var/log

This is fantastic when you're bouncing between two directories. It's like the "back" button.

Toggle Between Two

Working on a project and checking logs? Use cd - to toggle back and forth. Way faster than typing full paths.

Absolute vs Relative

hljs bash
# Absolute - start from root
cd /var/log

# Relative - start from current directory
cd logs/nginx

Both work. Use absolute when you know exactly where you're going. Use relative for nearby directories.

Common Patterns

Jump to a Deep Directory

Terminal
$cd /home/user/projects/website/src/components

Create and Enter

Terminal
$mkdir -p newproject/src && cd newproject/src
$pwd
/home/user/newproject/src

Quick Back-and-Forth

Terminal
$cd /etc/nginx
$# Check something
$cd -
/home/user/projects
$# Work on code
$cd -
/etc/nginx

Advanced: pushd/popd (Directory Stack)

If you're bouncing between more than two directories, use pushd and popd:

Terminal
$pushd /var/log
/var/log ~
$pushd /etc/nginx
/etc/nginx /var/log ~
$pushd /home/user/projects
/home/user/projects /etc/nginx /var/log ~
$popd
/etc/nginx /var/log ~
$popd
/var/log ~

pushd saves your current location to a stack before moving. popd pops you back. Use dirs to see the stack.

When to Use pushd

Great for temporarily jumping somewhere, doing work, then returning. Think: "I'll need to come back here." That's a pushd moment.

What cd Can't Do

cd only changes directories. It can't:

  • Change to a file (cd file.txt - error)
  • Create directories (use mkdir first)
  • Work if the directory doesn't exist
Terminal
$cd nonexistent
bash: cd: nonexistent: No such file or directory

Quick Reference

CommandWhere It Goes
cdHome directory
cd ~Home directory
cd ..One level up
cd ../..Two levels up
cd -Previous directory
cd /pathAbsolute path
cd pathRelative path
Knowledge Check

What does `cd -` do?

Key Takeaways

  • cd with no arguments goes home
  • .. goes up one level
  • - goes to previous directory (toggle mode)
  • Use absolute paths when you know the full location
  • Use relative paths for nearby directories
  • cd only works with directories, not files

Next: let's uncover the secret world of hidden files.