wc: Word Count
Sometimes you do not want to read a file. You want to measure it. How many lines? How many words? The wc command (word count) tells you.
Run plain wc on a file:
wc crew.txt
3 3 15 crew.txt
The three numbers are, in order:
- lines
- words
- characters (bytes)
Counting just one thing
Options narrow it down to a single number:
| Option | Counts |
|---|---|
wc -l | lines only |
wc -w | words only |
wc -c | characters only |
wc -l crew.txt
3 crew.txt
Why this is useful
wc -l is one of the most-used commands on Linux. "How many entries are in this
list?" "How many errors are in this log?" Count the lines and you have your answer.
Later you will feed other commands' output straight into wc to count results
automatically.
Mission: Count and File the Inventory
inventory.txt lists every item in the cargo bay, one per line. The quartermaster needs an exact count before the next resupply shuttle arrives, plus a filed copy of the manifest.
- Count how many lines are in
inventory.txt. That is the number of items. - Run a full measure of
inventory.txtto see its lines, words and characters together. - Create a folder called
manifestand copyinventory.txtinto it asinventory-checked.txt. - Confirm the filed copy has the same item count by counting its lines too.
Expected result
You see the item count and the full measurement, and the manifest folder holds a copy whose line count matches the original.