>_ IzzyLab

Hands-on task

This task is step 5 of the course “First steps with text”

Count the lines in the lists

easy~4 min

The /root/party directory has three guest lists for the weekend: friday.txt, saturday.txt and sunday.txt — one name per line. There are no repeats: each guest comes on one day.

How many guests are there in all three lists together? Write a single number to /root/answer.txt, for example like this: echo 10 > /root/answer.txt — just with your number.

What you'll practise:
  • wc -l
  • several files
  • the * pattern

Topic breakdown

wc (word count) counts the lines, words and characters in a text. Most often you need the -l flag — lines only. If a file has one record per line, that's the number of records.

wc -l /etc/passwd             # the line count and the file name
wc -l /etc/passwd /etc/group  # one line per file and a total

With several files, wc prints a total line at the end — the sum over all of them.

A pattern saves you from listing files one by one: * means "any characters", and the shell itself replaces /etc/*.conf with all the matching files.

What's checked

  1. answer.txt holds a single number and nothing else
  2. The number equals the count of guests in all three lists

What it looks like