Skip to content
Linux Basics: Pipes and Output Redirection

Linux Basics: Pipes and Output Redirection

October 5, 2026

Commands normally print their results in the terminal. In Bash on Debian and Ubuntu, redirection saves those results to a file, while a pipe sends them to another command.

Save and append text

Run mktemp -d, then use cd to enter the directory it prints. Try these commands there:

printf 'pear\napple\npear\n' > fruit.txt
printf 'banana\n' >> fruit.txt
cat fruit.txt

> creates a file or replaces its existing contents. >> adds text to the end instead. Check the destination name before using > because it does not ask before overwriting.

Here, printf writes one fruit per line. The \n sequences mark line breaks.

Connect two commands

sort fruit.txt | uniq
sort fruit.txt | uniq > unique-fruit.txt

The pipe symbol, |, sends the first command’s standard output to the second command’s standard input. sort puts matching lines together, and uniq removes adjacent duplicates.

The second example saves the result instead of displaying it. fruit.txt remains unchanged because the destination has a different name. Do not redirect a command’s output back into the same file it is reading: the shell may empty that file before the command reads it.

Read a file as input

wc -l < unique-fruit.txt
3

< supplies a file as standard input. wc -l counts newline characters. A final line without a newline is not counted.

Ordinary pipes and > carry standard output. Error messages use a separate stream called standard error, so they may still appear in the terminal.

Reference

Debian Bash manual

Acknowledgements

This post was created with assistance from AI.

Linux Basics — lesson 7 of 25. Series index

Previous: Finding Files with find · Next: Sorting and Counting Text Lines

Last updated on