TetSharing
Git & CLI tutorial · 12 chapters
  1. Terminal basics
  2. Working with files
  3. Pipes and redirection
  4. What Git is
  5. Staging and commits
  6. Viewing history
  7. Comparing changes
  8. Branches
  9. Merging
  10. Merge conflicts
  11. Undoing changes
  12. Remotes, push and pull

Final Git & CLI quiz
Cheat sheet: git commands
Cheat sheet: linux shell commands

Chapter 3 of 12 · By Ethan Cole · Updated November 21, 2026

Shell Pipes and Redirection: >, >> and | Explained

The Unix idea behind the shell is "small tools that each do one thing, joined together". Redirection connects a command to a file; a pipe connects one command to another.

Standard streams

Every command has three streams: standard input (what it reads, normally the keyboard), standard output (results, normally the screen) and standard error (error messages, also the screen). Redirection changes where these go.

Redirection

  • command > file sends standard output to a file, replacing its contents.
  • command >> file appends to the end of the file.
  • command < file feeds the file to the command's standard input.
  • command 2> errors.txt redirects standard error (stream number 2).

Pipes

The | character sends the output of one command straight into the input of the next, with no temporary file:

cat access.log | grep 404 | wc -l

reads a log, keeps the lines containing 404, and counts them. Each stage is simple; the combination answers a real question.

A toolkit of filters

  • grep word keeps lines containing word; -i ignores case, -c counts matches, -v inverts.
  • sort sorts lines alphabetically; -n sorts numerically, -r reverses.
  • uniq removes adjacent duplicates, so it is almost always used after sort; uniq -c counts them.
  • wc -l counts lines.
  • head -n 3 and tail -n 3 keep the first or last lines.

The example writes a small file of programming languages with printf, then answers questions about it with pipelines: how many lines mention Python, and which entries are most common.

Terminal · bash + git · copy and run locally
# printf writes one language per line into a file
printf 'Python\nJavaScript\nPython\nSQL\nPython\nJavaScript\n' > langs.txt
wc -l < langs.txt
grep -c Python langs.txt
grep -v Python langs.txt
sort langs.txt | uniq
sort langs.txt | uniq -c | sort -nr | head -n 2
echo "Go" >> langs.txt
tail -n 2 langs.txt

Transcript (verified by our test suite in a fresh temp folder)

$ printf 'Python\nJavaScript\nPython\nSQL\nPython\nJavaScript\n' > langs.txt
$ wc -l < langs.txt
       6
$ grep -c Python langs.txt
3
$ grep -v Python langs.txt
JavaScript
SQL
JavaScript
$ sort langs.txt | uniq
JavaScript
Python
SQL
$ sort langs.txt | uniq -c | sort -nr | head -n 2
   3 Python
   2 JavaScript
$ echo "Go" >> langs.txt
$ tail -n 2 langs.txt
JavaScript
Go

Read the last pipeline from left to right: sort the lines so duplicates sit together, count each group with uniq -c, sort those counts numerically in reverse, and keep the top two.

Why this matters for Git

Git commands are designed to work in pipelines too. git log --oneline | head -n 5 shows the five most recent commits, and git branch | grep feature finds feature branches. Once pipes feel natural, the terminal stops being a place for one command at a time.

Common mistakes

  • Using > when you meant >> and wiping a file.
  • Expecting uniq to remove duplicates that are not next to each other. Sort first.
  • sort numbers.txt putting 10 before 9: add -n for numeric order.

Summary

  • > replaces, >> appends, < reads, 2> captures errors.
  • | chains commands; build pipelines one stage at a time.
  • sort | uniq -c | sort -nr is the classic "most common" recipe.

Quiz: Pipes and redirection

1. What does ls >> list.txt do?

>> appends; > would replace the contents.

2. Why is uniq usually run after sort?

Sorting groups identical lines together so uniq can collapse them.

3. In grep error app.log | wc -l, what does the pipe do?

A pipe connects standard output of one command to standard input of the next.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.