🎓 A1Learner
🔍
0 of 50 lessons completed

Counting with wc

Wc counts the lines, words, and bytes in a file. A byte is one unit of storage, like one letter.

Count the lines

Three files, three line counts.

$ cd ~/practice
$ wc -l projects/report.txt
$ wc -l data/logs.txt
$ wc -l data/numbers.txt

Count the words

The word counts are larger than the line counts.

$ cd ~/practice
$ wc -w projects/report.txt
$ wc -w data/logs.txt

Count the bytes

The byte counts are larger still.

$ cd ~/practice
$ wc -c projects/report.txt
$ wc -c data/logs.txt

All three counts at once

Wc with no option prints lines, words, and bytes in that order.

$ cd ~/practice
$ wc data/logs.txt

Example

$ cd ~/practice
$ wc -l projects/report.txt data/logs.txt data/numbers.txt

Key points

  • Use wc -l to count the lines in a file.
  • Use wc -w to count the words in a file.
  • Use wc -c to count the bytes in a file.
  • Wc with no option prints all three counts at once.
  • The counts sit before the file name.
  • Wc turns the size of a file into three plain numbers.

In this example, wc counts the lines in three files at once. The report holds 4 lines, the log holds 5, and the numbers hold 6. The last line adds them up to 15.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

How many lines does data/logs.txt hold?