Skip to content

02 - The Shell: ls, cd, pwd

What this session is

About 30 minutes. You'll learn to navigate the Linux filesystem - find where you are, move around, list what's there.

The filesystem is a tree

Linux organizes files in a tree. The top is / ("root"). Everything is under it.

/
├── bin/        commands available to all users
├── etc/        system configuration files
├── home/       user home directories
│   ├── alice/
│   └── bob/
├── tmp/        temporary files (cleaned periodically)
├── usr/        installed software
├── var/        variable data (logs, mail)
└── ...

(macOS uses /Users/alice instead of /home/alice - that's the main difference.)

Your home directory is where your personal files live. Always written ~ (tilde) as shorthand: ~/Documents = /home/yourname/Documents.

pwd: where am I?

pwd

Prints your current directory. Probably /home/yourname (your home) when you open a fresh terminal.

cd: change directory

cd /etc        # go to /etc
pwd            # /etc
cd ~           # back to home
pwd            # /home/yourname
cd /           # go to root
cd             # alone, also goes home

Special destinations: - cd ~ - your home. - cd / - the filesystem root. - cd .. - up one level. - cd - - back to the previous directory you were in.

Relative vs absolute paths: - Absolute start with /: /etc/hosts, /usr/bin/ls. - Relative don't, and are relative to your current directory: Documents/notes.txt means "from where I am, into Documents, then notes.txt."

Special relative shortcuts: - . - current directory. - .. - parent directory.

cd /etc
cd ..          # now in /
cd ./tmp       # same as cd /tmp from /

ls: list contents

ls             # files and folders in current directory
ls /etc        # contents of /etc
ls -l          # long format (permissions, owner, size, date)
ls -a          # include hidden files (start with .)
ls -h          # human-readable sizes (with -l: 4.0K, 1.2M)
ls -la         # combine: long format + hidden

You'll meet hidden files often. Anything starting with . is conventionally "config" or "internal": .bashrc, .gitignore, .vscode/. Not actually hidden - just convention-hidden.

A tour: walk through /

cd /
ls

You should see directories like bin, etc, home, tmp, var, usr. Each has a purpose:

Directory What lives here
/bin and /usr/bin Commands available to all users (ls, cat, grep, ...)
/sbin and /usr/sbin Commands for system administration (mount, ifconfig)
/etc System-wide configuration files
/home User home directories
/tmp Temporary files; often cleared on reboot
/var Variable data - logs (/var/log), mail spools, etc.
/usr Installed software (binaries, libraries, docs)
/opt Third-party software (sometimes)
/proc Virtual filesystem exposing kernel info
/sys Virtual filesystem for kernel objects
/dev Device files (disks, terminals)
/root Home directory of the root (admin) user

Most of this you don't need to touch as a beginner. Knowing the layout helps you predict where to find things.

File metadata: ls -l

ls -l ~

Output looks like:

drwxr-xr-x  3 alice alice 4096 May 17 10:23 Documents
-rw-r--r--  1 alice alice  128 May 16 14:11 notes.txt

Each row, left to right:

Column Meaning
drwxr-xr-x Type + permissions (page 06)
3 Number of links (usually 1 for files)
alice Owner
alice Group
4096 Size in bytes
May 17 10:23 Last modified
Documents Name

The first character is the type: - d - directory - - - regular file - l - symbolic link - b c s p - special files (you'll rarely see these)

The next 9 characters are permissions. We'll cover them in page 06.

Tab completion saves your life

Type:

cd ~/Doc

Press Tab. The shell completes to ~/Documents/ (if it exists). Saves typing; prevents typos.

If multiple things match (~/D and you have Documents and Downloads), pressing Tab twice shows both.

Exercise

In your terminal:

  1. pwd - note where you are.
  2. cd / - go to root.
  3. ls - see the top-level directories.
  4. cd /etc - go to etc.
  5. ls | head - see the first ~10 entries (the | and head are page 04 and 07; for now, just try).
  6. cd ~ - back home.
  7. ls -la - long-format listing including hidden files. Count how many of your home's items are hidden (start with .).
  8. cd ~/Documents (if it exists; else create it: mkdir ~/Documents and then cd).
  9. cd .. - back to home.
  10. cd - - back to Documents.

Spend 10 minutes wandering. cd somewhere, ls, pwd, cd ... Build muscle memory.

What you might wonder

"Why both /bin and /usr/bin?" Historical. The split made more sense when /usr was on a separate disk. Modern distros often merge them; the duplication is for compatibility.

"What's . and .. actually?" Every directory contains two implicit entries: . (itself) and .. (its parent). ls -a / shows them.

"Why is my home /Users/alice on macOS but /home/alice on Linux?" macOS does it differently. ~ works on both as the abbreviation.

"Should I cd by typing the full path or by hopping?" Whichever is faster. Tab completion + history makes long paths fast. Use absolute paths when you want certainty.

Done

  • Recognize the filesystem tree.
  • Use pwd, cd, ls (with -l, -a, -h).
  • Distinguish absolute and relative paths.
  • Use ., .., ~, - as shortcuts.
  • Read ls -l output.

Next: Files and directories →

Comments