Lesson 3.6: More ways to look at text: head, tail, wc, sort and uniq
The classic small tools: the beginning and the end of a file, counting lines and words, sorting alphabetically and numerically, and removing or counting duplicates; a first taste of combining them with |.
17 min
You will learn to
- Show the first or last lines of a file with head and tail (-n)
- Count lines, words and bytes with wc
- Sort text, numbers and by a column with sort (-n, -r, -k)
- Remove and count duplicates with uniq (after sort)
Key terms
- head / tail
- Print the first / last lines of a file, 10 by default; -n N chooses how many.
- wc
- Word count: counts lines (-l), words (-w) and bytes (-c).
- sort
- Sorts lines. -n numerically, -r in reverse, -k N by the Nth column, -u removing duplicates.
- Lexicographic vs numeric order
- Without -n, sort compares characters one by one, so 10 comes before 9 (1 < 9). With -n it compares the numbers' values.
- uniq
- Removes repeated lines that are next to each other; -c prefixes each line with its count. Sort first so equal lines are neighbours.
- Pipe |
- Connects the output of one command to the input of the next.
← Wildcards and path expansion · All lessons · Hard and symbolic links →