Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 95 ยท Getting Started

Navigating: pwd, cd and ls

Learn to move around the Linux filesystem with pwd, cd and ls - options -l, -a, -h, -R, -t and -S, reading ls -l output, and the working directory of every process.

Three commands carry you through almost every session on a Linux machine: pwd tells you where you are, cd moves you somewhere else, and ls shows what is there. They look trivial, yet the details (relative paths, cd -, the columns of ls -l, the sort options) are what separate hesitant typing from fluent work.

This lesson builds on the directory tree from The Linux Filesystem. You will learn each command with its most useful options, how to read the long listing format line by line, how a program's working directory works, and the interactive time-savers (tab completion and history) that you will use at a real prompt. The next lesson, Creating and Viewing Files, puts something in those directories.

pwd: where am I?

pwd prints the working directory: the directory your shell is currently in and the starting point for every relative path. In a script it is also available in the variable $PWD. Because absolute sandbox paths vary, our examples print only the last name of the directory.

Every process has its own working directory. When the shell starts another program, the program inherits the directory as it is at that moment, but afterwards the two are independent: a script that does cd /tmp does not move the shell that started it. We come back to this in Subshells.

cd: change directory

cd takes one argument, the destination, which can be absolute or relative. A few special forms save a lot of typing:

Command Goes to
cd /etc The absolute path /etc
cd docs The docs directory inside the current one
cd .. The parent directory
cd ../.. Two levels up
cd or cd ~ Your home directory
cd - The previous directory (and prints it)

cd - is based on the variable OLDPWD, which the shell updates on every cd. It lets you jump back and forth between two places, which is perfect when you are comparing two locations.

Bash Moving around with cd

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Step by step: cd lab enters lab; alpha/deep is two levels down in one go; ../.. climbs two levels back to lab. After cd beta, cd - returns to the previous directory (lab) and prints where it went: that is the extra line lab in the output, shown relative to the sandbox directory (on a normal system it is a full absolute path). The second cd - swaps back to beta and prints lab/beta. Finally cd ~ goes home; in our sandbox the home directory is a folder named .home.

What happens when a path is wrong? cd fails with an error and, importantly, stays where it is. In a script that is dangerous: a failed cd followed by rm -rf * removes files in the wrong place. Always write cd somewhere || exit 1, or use set -e (Exit Codes and Error Handling).

Bash A failing cd leaves you where you are (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The error is main.sh: line 3: cd: /no/such/place: No such file or directory, the status is 1, and the working directory is unchanged. The last line shows the defensive pattern: a failed cd triggers the || branch, where a real script would stop.

ls: listing a directory

ls lists the names in one or more directories (the current one by default). The options you will use all the time:

Option Effect
-l Long format: permissions, owner, size, date, name
-a Include hidden files (and . and ..)
-A Hidden files, but without . and ..
-h Human-readable sizes (4.0K, 1.5M), together with -l
-R Recurse into sub-directories
-t Sort by modification time, newest first
-S Sort by size, largest first
-r Reverse the sort order
-d List a directory itself, not its contents
-1 One name per line
-F Add a marker: / for directories, * for executables

Options combine, so ls -lAh is a common first move on an unknown directory and ls -ltr (long, by time, reversed) puts the newest file at the bottom, which is where your eyes are when the listing ends.

Bash Sorting, markers and recursion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

With -F the directories get a trailing slash: docs/ and src/. Plain ls docs lists the contents, while ls -d docs prints docs only, which matters when combined with wildcards. -R shows each directory under a name: header, starting with .: for the current one. -S puts the longer b.txt before a.txt.

Reading ls -l

The long format packs many facts into each line. To keep our output stable we omit the owner and group with -g -o and print the time in an unambiguous format.

Bash Anatomy of a long listing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Each line has these columns, left to right:

TEXT
-rwxr-xr-x 1 18 2026-03-14 09:30 run.sh
|\_______/ | |  \__________/    \_____/
|   |     | |       |             +-- name
|   |     | |       +---------------- last modification time
|   |     | +------------------------ size in bytes
|   |     +-------------------------- number of hard links
|   +-------------------------------- permissions: user, group, others
+------------------------------------ type: - file, d directory, l symlink

The first character is the file type (- regular file, d directory, l symbolic link). The next nine are the read/write/execute permissions for user, group and everyone else, which File Permissions explains. The number is the hard link count (Links), then comes the size in bytes, the modification time and the name. A normal ls -l also shows the owner and the group, between the link count and the size; we hid them for stable output. The directory sub shows d and a size of 4096, the space the directory entry itself needs, not its contents. (The s in its permissions is the setgid bit that the sandbox directory passes on to new sub-directories; on your machine you will normally see x there.) The total 12 line is the disk blocks used, in KiB, and you can ignore it. With -h the 1536-byte file is shown as 1.5K.

Do not parse ls output in scripts. File names can contain spaces, newlines and other surprises, and the format depends on options and locale. Use globs (for f in *.txt), find, or stat instead (Wildcards and Globbing, Finding Files).

Time and size ordering in practice

Sorting by time and size answers the questions you ask most often: what changed lately, and what is large? Our files use fixed timestamps so that the result is the same on every run.

Bash The newest file and the biggest file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

ls -t lists new.log mid.log old.log and ls -S the same here because the newest file is also the biggest. Joining the lines with tr keeps the example compact. Piping to head -n 1 extracts a single answer; using ls output this way is fine for quick interactive checks, but not for scripts that must handle any file name.

A tree-like view

The tree command draws a directory as an indented tree, but it is often not installed (it is not in our sandbox). find plus sed can approximate it, and you will master find in Finding Files.

Bash A poor man's tree

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

find site prints every path below site, sort orders them, and the sed command replaces each name/ prefix with two spaces so that depth becomes indentation. It is crude, but it works on any system that has these three standard tools.

Interactive helpers (read-only)

At a real prompt you rarely type full paths. These features need an interactive shell, so they are shown as read-only text rather than runnable examples.

  • Tab completion. Type the first letters of a command or path and press Tab. The shell completes it, or lists the choices on a second press. This is the single biggest time saver and also prevents typos.
  • History. The Up and Down arrows walk through earlier commands. Ctrl+R starts a reverse search: type a few letters from an old command and press Ctrl+R again for older matches. The history command lists them, and !! repeats the previous line.
  • Line editing. Ctrl+A and Ctrl+E jump to the start and end of the line, Ctrl+W deletes a word, Ctrl+U deletes to the start and Alt+. inserts the last argument of the previous command.
CONSOLE
$ cd /etc/ap<Tab>        # completes to /etc/apt/
$ !!                     # repeat the last command
$ sudo !!                # repeat it with sudo in front
$ cd -                   # back to where you were

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does cd - do?

    cd - switches to OLDPWD, the directory you were in before the last cd, and prints it.
  2. 2. What happens to the working directory when cd fails?

    A failed cd prints an error and leaves you where you were. In scripts, handle the failure with || exit 1 to avoid acting in the wrong place.
  3. 3. Which command lists the newest file last?

    -t sorts newest first, and -r reverses it, so -ltr puts the newest entry at the bottom.
  4. 4. In the first column of ls -l, what does a leading d mean?

    The first character gives the type: - file, d directory, l symbolic link.
  5. 5. Why should you avoid parsing ls output in scripts?

    Use globs, find or stat, which handle any file name safely.

Summary

  • pwd prints the working directory; every process has its own, inherited at start and independent afterwards.
  • cd path moves, cd .. goes up, cd goes home, cd - returns to the previous directory; a failing cd leaves you where you are.
  • ls -lAh is the everyday listing; -R recurses, -t and -S sort by time and size, -r reverses, -d shows the directory itself, -F marks types.
  • In ls -l the columns are type and permissions, link count, owner, group, size, time and name.
  • Never parse ls in scripts; use globs, find or stat.
  • Tab completion, history and line editing make interactive work fast.

Next: Creating and Viewing Files