The wc command stands for "word count." When you add the -l flag, it specifically counts the lines in a file.
How it works:
wc -l filename
Example Output:
15 filename
This means there are 15 lines in that file.
Why is it useful with the cat and redirection?
-
Verify Append (
>>):
If you want to be sure that your append command worked, you can check the line count before and after:- Before:
wc -l myfile(Result: 10) - Run:
cat extra_data >> myfile - After:
wc -l myfile(Result: 11) — Success!
- Before:
-
Check for Empty Files:
Ifwc -lreturns0, you know the file is empty. -
Count Multiple Files:
You can even use it on multiple files at once:wc -l part1.txt part2.txtIt will show you the count for each file and a total at the bottom.
Other wc flags you might like:
wc -w: Counts the words.wc -c: Counts the bytes (similar to file size).wc(without flags): Gives you lines, words, and bytes all at once.
Try it out:
Try running wc -l /tmp/hello. Based on the file content you saw earlier:
Hi,
I am Labby!
It should return 2!