Linux Labs

First steps / Stage 12 of 15

Pipelines: an answer from several commands

Not running Starting usually ~20 s
Leave the lesson

Step 4 of 7 · theory · 6 min

The most frequent on top

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.

Check yourself 1 question

The file sizes.txt has three lines — 9, 10 and 100. In what order does sort sizes.txt print them?

  • sort -rn puts the big numbers on top. With no options, sort compares text, and the lines starting with 1 come before the one starting with 9.

  • sort sorts any lines, numbers included, but as text: 10 and 100 start with 1 and come before 9.

  • Without -n, sort compares text character by character: 1 is less than 9, so 10 and 100 come before 9. sort -n puts 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.

Check yourself 1 question

/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 denied messages 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 -rn puts the big numbers on top, and head -n 3 keeps the top three lines.
  • A pipeline is built one link at a time, checking the output through head after each.
  • Only stream 1 goes through |; errors are redirected with a 2> on their own command.
Ubuntu 24.04
Loading…