pwd and cd

pwd prints your current folder and cd moves you to another one.

Syntaxcd [directory] pwd

In Linux everything is organised into folders (called directories). At any moment your shell sits inside one directory, known as the current working directory.

pwd — print working directory

pwd tells you exactly where you are.

cd — change directory

cd moves you to a different directory. A few handy shortcuts:

  • cd /etc — go to a specific directory.
  • cd .. — go up one level to the parent.
  • cd ~ or just cd — go to your home directory.
  • cd - — go back to the directory you were just in.

Example

Example · bash
pwd
# Output: /home/alice

cd /var/log
pwd
# Output: /var/log

cd ..
pwd
# Output: /var

cd            # back to home
pwd
# Output: /home/alice

When to use it

  • A sysadmin scripts a deployment that calls 'pwd' at each stage to log the working directory for audit trails.
  • A developer uses 'cd -' to toggle quickly between a source directory and a build output directory.
  • A new Linux user runs 'cd ~' to return to their home directory after accidentally navigating deep into /etc.

More examples

Print and navigate directories

Shows 'pwd' reporting the current directory before and after a 'cd' to an absolute path.

Example · bash
pwd
# /home/alice

cd /var/log
pwd
# /var/log

Navigate with relative paths

Uses relative paths and the parent-directory shortcut '..' to move without typing full paths.

Example · bash
cd /var/log
cd nginx          # go into subdirectory
pwd
# /var/log/nginx

cd ..             # go up one level
pwd
# /var/log

Toggle between two directories

Demonstrates 'cd -' which alternates between the current and previous directory like a back button.

Example · bash
cd ~/projects/api
cd /var/log
cd -              # jump back to ~/projects/api
# /home/alice/projects/api
cd -              # back to /var/log
# /var/log

Discussion

  • Be the first to comment on this lesson.