What is left is to order the counts from high to low and keep the top of the list.
sort compares lines as text, character by character. Numbers need the -n option: with it, 9 is less than 10. The -r option reverses the order, so the big numbers go on top. That is why sort -rn after uniq -c puts the most frequent address first, and head -n 3 keeps the top three lines.
The file sizes.txt has three lines — 9, 10 and 100. In what order does sort sizes.txt print them?
-
sort -rnputs the big numbers on top. With no options, sort compares text, and the lines starting with1come before the one starting with9. -
sort sorts any lines, numbers included, but as text: 10 and 100 start with
1and come before 9. -
Without
-n, sort compares text character by character:1is less than9, so 10 and 100 come before 9.sort -nputs them in order of size.
With no options, sort compares lines as text: 10 and 100 start with 1 and come before 9. sort -n orders them by size — 9, 10, 100.
A long pipeline is built one link at a time: add a command, look at the output through | head, then write the next one. That way you see at once at which link the lines went wrong.
Only stream 1 enters a pipeline. Each command's error messages go past it, straight to the screen, and you redirect them with a 2> on the command in question. The finished answer is saved with > at the end of the pipeline: … | head -n 3 > top.txt.
/etc has directories that student may not look into. What ends up on the screen after find /etc -name '*.conf' | wc -l?
-
find prints the
Permission deniedmessages to stream 2, past the pipeline, so they are on the screen as well. wc counted just the paths. -
wc got the paths from find through
|, so it waits for no keyboard input and prints their count. -
The messages never enter the pipeline: they are in stream 2. wc counted the paths, and the messages were printed on the screen.
A pipeline passes stream 1 alone, so wc counted the paths found, while find printed the refusal messages straight to the screen. A 2> /dev/null on find removes them.
Key points
-
sort -rnputs the big numbers on top, andhead -n 3keeps the top three lines. -
A pipeline is built one link at a time, checking the output through
headafter each. -
Only stream 1 goes through
|; errors are redirected with a2>on their own command.