Piping
Objectives
By the end of this chapter, you should be able to:
- Explain what
head,tail,sort,uniq,wc, andgrepdo - Define what piping is
- Chain commands together with
|to solve problems no single command handles alone
๐ก Why this matters: Redirection connects a command to a file. Piping connects a command directly to another command โ that’s the difference between typing one command at a time and building a small pipeline, which is most of what real command-line work actually looks like.
A Few Commands Worth Knowing First
headโ prints the first lines of a file (-npicks how many)tailโ prints the last lines of a file (-npicks how many)sortโ sorts a file’s linesuniqโ removes duplicate lines, but only if they’re already sorted and adjacentwcโ counts words, lines, characters, and bytes
Combining Files with cat
Given first.txt:
First
Second
Third
and second.txt:
Fourth
Fifth
Sixth
cat first.txt second.txt concatenates them โ prints both, one after the other, as if they were a single file.
Piping
What if you want to concatenate two files and then count the words? Or concatenate and then sort? You need a way to feed one command’s output straight into another โ that’s what a pipe (|) does. Think of it as a hose connecting the output of the command on the left to the input of the command on the right:
cat first.txt second.txt | sort
Pipes chain as many times as you want:
cat first.txt second.txt | sort | head -n 2
Try reasoning through this one before running it:
cat first.txt second.txt | sort | tail -n 3 | head -n 1
Step by step: concatenate the two files, sort the result, keep the last three lines, then keep just the first of those three. Net effect: the third-from-last line of the combined, sorted output โ without ever knowing how many lines the file has.
Searching with grep
grep searches text for a pattern, and it’s most useful piped in after something else. cat first.txt | grep First prints First โ the one line that matched. No match means no output at all; multiple matches means every one gets printed. Try:
cat first.txt second.txt | grep Nope
cat first.txt second.txt | grep th
Putting It Together
Say petnames.txt contains:
Lassie
Moxie
Whiskey
Fido
Lassie
Moxie
Plenty of duplicates. Running uniq petnames.txt directly doesn’t remove them, because uniq only catches duplicates that are already next to each other โ the file needs sorting first:
sort petnames.txt | uniq
Fido
Lassie
Moxie
Whiskey
Add redirection on top and you can send that straight to a new file instead of just printing it:
sort petnames.txt | uniq > petnames_sorted.txt
Quick Reference
| Command | Does |
|---|---|
head -n N |
First N lines |
tail -n N |
Last N lines |
sort |
Sorts lines alphabetically |
uniq |
Removes adjacent duplicate lines (input must be sorted) |
wc |
Counts lines, words, characters |
grep pattern |
Prints only lines matching pattern |
Try It
Pipes run against real files, so try these yourself:
- Create two short text files and use
catand|to combine and sort them in one line. - Use
wcpiped after something else to count how many lines contain a specific word. - Recreate the
petnames.txtexample with your own list of duplicated words, then sort, dedupe, and redirect the result to a new file โ all in one line.
Recap
|connects the output of one command to the input of the next, and you can chain several together.head/tailgrab lines from one end of a file,sort/uniqclean up ordering and duplicates,wccounts, andgrepfilters by pattern.- Piping and redirection combine freely โ a pipeline can end by printing to your screen or writing to a file.
Next module: environment variables, the PATH, and how to find and manage running processes.