What does `wc -l` do?

QuestionsQuestions8 SkillsProDec, 18 2025
0108

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?

  1. 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!
  2. Check for Empty Files:
    If wc -l returns 0, you know the file is empty.

  3. Count Multiple Files:
    You can even use it on multiple files at once:

    wc -l part1.txt part2.txt

    It 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!

0 Comments

no data
Be the first to share your comment!