Coding for All
Level 18: Counting Lines
Counting Lines Count how many things are in a file.

wc is short for "word count". On its own it counts lines, words and characters all at once; with the -l flag it counts lines only.

The answer for shopping.txt is 8, which is the number of items on the list, because this file keeps one item per line. That is why "one thing per line" is such a common way to store things: it makes counting them free.

wc repeats the filename after the number, so you can count several files at once and still tell the answers apart. Try wc -l *.txt.

wc -l shopping.txtCount the lines.
wc shopping.txtLines, words and characters.
wc -l *.txtCount several files at once.
Your task
  • Count the lines in shopping.txt
  • Count the lines in every .txt file at once

Open this level in your browser to type the commands at a live terminal that answers back.