Pipes and redirection in bash
Updated · Every command on this page ran in our practice shell
Short answer
> writes a command's output to a file (replacing it), >> appends to the end, 2> sends error messages to a file and 2>/dev/null discards them. > file 2>&1 (or &> file) captures both. A pipe | sends one command's output into the next, as in grep ERROR app.log | wc -l, and tee saves a copy on the way.
Every command has an input and two outputs: normal output (stdout) and error messages (stderr). Redirects send those outputs into files; pipes connect one command's output to the next command's input. Together they turn small commands into powerful one-liners.
Write output to a file with >
command > file sends the output into file instead of the screen. If the file exists, it's replaced without warning.
learner@shellstreak:~$ ls projects/website > site-files.txt && cat site-files.txt README.md index.html style.css
Append to a file with >>
>> adds to the end of the file and keeps what's already there. Use it for logs, notes and settings files.
learner@shellstreak:~$ echo "mend the fence" >> todo.txt && cat todo.txt write cover letter fix bike light water the plants mend the fence
Send errors to a file with 2>
Error messages travel on stream 2, so > doesn't catch them. 2> errors.txt does.
learner@shellstreak:~$ cat logs/missing.log 2> errors.txt; cat errors.txt cat: logs/missing.log: No such file or directory
Capture both streams: > file 2>&1 and &>
cmd > run.log 2>&1 sends stdout to the file, then points stderr at the same place. Bash's shorthand is cmd &> run.log. Order matters: in cmd 2>&1 > run.log, stderr is pointed at the screen (where stdout was at that moment), so only normal output reaches the file.
learner@shellstreak:~$ cat logs/error.log logs/missing.log > run.log 2>&1; cat run.log ERROR 2026-03-02 09:15:22 page not found: /menu.html WARN 2026-03-02 09:18:55 forbidden: /admin ERROR 2026-03-02 09:19:12 page not found: /menu.html ERROR 2026-03-02 09:20:30 order failed: oven timeout INFO 2026-03-02 09:21:00 oven restarted cat: logs/missing.log: No such file or directory
Hide errors with 2>/dev/null
/dev/null is a special file that discards everything written to it. 2>/dev/null keeps the normal output and silences the errors. Only discard errors you've already read and understood.
learner@shellstreak:~$ cat notes/ideas.md notes/plan.md 2>/dev/null Seasonal pie menu TODO: price the pies Loyalty stamps
Chain commands with a pipe
a | b feeds the output of a into b. Each command does one small job: here grep picks the matching lines and head keeps the first two.
learner@shellstreak:~$ grep index.html logs/access.log | head -n 2 2026-03-02 09:14:01 GET /index.html 200 2026-03-02 09:17:03 GET /index.html 200
Sort and count with sort | uniq -c | sort -rn
uniq -c counts repeated lines, but only neighbours, so sort first. A final sort -rn puts the biggest count on top. This pattern answers "what's most common?" for any list.
learner@shellstreak:~$ sort data/votes.txt | uniq -c | sort -rn 4 sourdough 3 rye 2 focaccia 1 bagel
Pick out columns with cut
cut -d, -f2 splits each line on commas and keeps field 2. Pipe it into the counting pattern above to summarise a CSV column; tail -n +2 skips the header row first.
learner@shellstreak:~$ cut -d, -f3 data/orders.csv | tail -n +2 | sort | uniq -c | sort -rn 3 croissant 2 sourdough 2 rye
Save and see output at once with tee
tee file writes its input to a file and passes it along the pipe, so you can save an intermediate result and keep going. tee -a appends instead of replacing. It's also how you write a root-owned file: sudo echo x > /etc/f fails because your own shell opens the file, but echo x | sudo tee -a /etc/f works.
learner@shellstreak:~$ grep 404 logs/access.log | tee notfound.txt | wc -l 2
Check yourself
One real exercise from the “Pipes & redirects” unit. No account needed.Add mend the fence as a new last line of todo.txt, keeping everything already in it.
What's checked: the file todo.txt.
Common mistakes
Using
>when you meant>>and wiping a file.>always starts the file from empty.Running
sort file > file. The shell emptiesfilebefore sort reads it, so you end up with nothing. Write to a new file instead.Using
uniqwithoutsortfirst: it only removes duplicates that are next to each other.Expecting
>to capture error messages. Those need2>.
Questions
What's the difference between | and >?+
`|` sends output to another **command**; `>` sends it to a **file**.
What does 2>&1 mean?+
Send stream 2 (errors) to the same place as stream 1 (normal output) is going right now. `cmd > all.txt 2>&1` puts both in one file; `cmd 2>&1 > all.txt` doesn't.
Is /dev/null a real file?+
It's a special device file on Linux and macOS. Anything written to it disappears, and reading from it returns nothing.
Reading is step one. Now make it stick.
Practise this in a five-minute lesson with instant feedback. No account needed.