Everyone Can Learn Program
中文 Start learning
Core · LinuxLesson 3 of 6Free to read

Pipes and redirection · Linux and the Command Line

How do you combine small commands into a powerful one?

Course contents

Why you need it

"Which city has the most users?" "Which error happens most often?" There is no single command for these, and there doesn't need to be. Linux tools each do one small job and pass text along, so you can build the answer in one line in a minute.

The intuition

The pipe | connects the output of one command to the input of the next, like a conveyor belt:

cut -d, -f2 data/users.csv | sort | uniq -c

take column 2 → sort it → count repeated neighbours.

Redirection sends output to a file instead of the screen: > replaces the file, >> adds to the end.

Definition

Pipe

`a | b` runs a and feeds everything it prints into b. Chains can be as long as you like.

sort, uniq, cut

`sort` orders lines (`-n` by number, `-r` reversed, `-t, -k2` by the 2nd comma-separated field). `uniq -c` counts repeated **neighbouring** lines. `cut -d, -f2` keeps field 2.

Examples

Users per city

Run it in the lesson

Skip the header with tail -n +2 (from line 2 on), then count.

tail -n +2 data/users.csv | cut -d, -f2 | sort | uniq -c | sort -nr
Step by step
  1. L1London 2, Shanghai 2, Berlin 1; the last sort puts the biggest counts first.2 Shanghai

Save results to files

Run it in the lesson

> creates or replaces; >> appends. Then read the files back.

grep ERROR logs/app.log > errors.txt
grep WARN logs/app.log >> errors.txt
wc -l errors.txt
grep -c timeout errors.txt
Step by step
  1. L1Nothing on screen: the three lines went into errors.txt.
  2. L2>> adds the WARN line at the end.
  3. L33 + 1 = 4 lines.4 errors.txt

Common mistakes

uniq without sort

uniq only merges lines that are next to each other. a b a stays three lines; sort first.

> when you meant >>

> empties the file before writing. A typo can wipe a log you wanted to keep.

Quick quiz

1. What does this print?

grep ERROR logs/app.log | cut -d' ' -f3- | sort | uniq -c | sort -nr | head -n 1
  1. A
          2 payment timeout
  2. B
          1 database connection lost
  3. C
          3 ERROR
Show the answer

Answer: A

cut keeps the words after ERROR. payment timeout appears twice, the most.

2. What does this print?

echo one > f.txt
echo two > f.txt
echo three >> f.txt
cat f.txt
  1. A
    two
    three
  2. B
    one
    two
    three
  3. C
    three
Show the answer

Answer: A

The second > replaced one with two, and >> added three.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictOldest users

    Type exactly what this prints.

  2. ModifyCount, don't list

    The pipeline lists every city, repeats included. Change it so each city appears once with its count, most users first.

  3. WriteAn error report

    Write the lines of logs/app.log that contain ERROR into report.txt, then append one more line: the number of those lines (grep -c). Print nothing on screen.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
Pipe管道| sends one command's output into the next command's input.
Redirection重定向> or >> sends output into a file instead of the screen.
CSVCSV 文件Comma-separated values: a table as plain text, one row per line.