>_ IzzyLab

Hands-on task

This task is step 9 of the course “First steps in Linux”

How many errors?

easy~4 min

Your boss asks: how many errors were there in the log /root/logs/app.log? It has three hundred lines, and counting by eye would take ages.

Count the lines that contain ERROR and write a single number to /root/count.txt. Try to do it in one line: find the lines and count them right away by joining two commands with the | sign.

What you'll practise:
  • wc -l
  • pipe |
  • grep
  • pipelines

Topic breakdown

The big idea of the command line is small commands that plug into each other. The | sign (a pipe) takes what the command on the left would print to the screen and feeds it to the input of the command on the right.

wc (word count) counts lines, words and characters; the -l flag counts only lines. An example unrelated to the task:

ls /etc | wc -l          # how many names are in /etc
cat /etc/passwd | wc -l  # how many lines are in passwd

A chain can have as many links as you like, and at the end you can send the whole thing to a file with >.

What's checked

  1. File /root/count.txt created
  2. count.txt holds one number and nothing else
  3. The number matches the count of ERROR lines

What it looks like