Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

>_
Terminal & Command Line
0/10 completed
Beginner
What is the terminal?Navigating foldersWorking with files and foldersViewing files
Intermediate
Wildcards and finding filesPipes and redirectionText processing: grep, sort, cut and friends
Advanced
Environment variables and PATHPermissions and processes
Pro
Shell productivity: aliases, scripts and dotfiles
Tracks / Terminal & Command Line / Intermediate

Pipes and redirection

This is where the terminal becomes magic. The Unix philosophy is: write small programs that do one thing well, and connect them together. Pipes and redirection are the connectors.

Three streams

Every command has three standard streams:

Stream Name Default Number
stdin Standard input Keyboard 0
stdout Standard output Screen 1
stderr Standard error (error messages) Screen 2

Redirection and pipes re-route these streams.

Redirecting output to files: > and >>

Linux terminal (simulator) · editable
Operator Does
cmd > file Write stdout to a file, replacing its contents
cmd >> file Append stdout to the end of a file
cmd < file Read stdin from a file
cmd 2> file Write errors to a file
cmd 2> /dev/null Throw errors away (/dev/null is a black hole)
cmd > out.txt 2>&1 Send both output and errors to one file (real bash)
Linux terminal (simulator) · editable

⚠️ > overwrites without asking. Typing > when you meant >> is a classic way to lose a file's contents.

Pipes: |

A pipe sends the output of one command into the input of the next:

Linux terminal (simulator) · editable

Read it left to right like a factory line:

cat names.txt  →  sort  →  uniq  →  wc -l
 (all names)    (sorted)  (unique)  (count them)

Each program knows nothing about the others. That's what makes them combinable in endless ways.

Building pipelines step by step

The pro technique: add one stage at a time and check the output each time.

Question: Which name appears most often in names.txt?

Linux terminal (simulator) · editable

uniq -c counts adjacent duplicates, which is why we sort first. sort -rn sorts numerically in reverse, so the biggest count is at the top.

More pipeline examples

Linux terminal (simulator) · editable

Chaining commands: ;, && and ||

These aren't pipes (no data flows between the commands), but they're just as useful:

Operator Runs the next command… Example
a ; b Always cd /tmp ; ls
a && b Only if a succeeded mkdir build && cd build
a || b Only if a failed cd project || echo "No project folder"
Linux terminal (simulator) · editable

Every command returns an exit status: 0 = success, anything else = failure. $? holds the last one. Scripts and CI pipelines depend on this.

Practice

  1. Save a sorted list of every file in Downloads to ~/downloads-list.txt.
  2. Count how many unique names are in data/names.txt.
  3. Append today's date to a file called log.txt three times, then show it with line numbers.
  4. Write one line that creates a folder reports and, only if that works, creates an empty reports/summary.txt inside it.
Linux terminal (simulator) · editable
✎ Check your understanding

1.What's the difference between > and >>?

2.In cat names.txt | sort | uniq -c, why is sort needed?

← Wildcards and finding files