Lesson 14 of 17
Counting Lines
Word Count
wc (word count) counts lines, words, and characters in a file or stdin.
Basic Usage
wc notes.txt
Output:
3 6 34 notes.txt
The three numbers are: lines, words, characters (bytes).
Counting Only Lines
The -l flag counts only lines:
wc -l notes.txt
Output:
3 notes.txt
The number is the count of newline characters. A standard text file with 3 lines has 3 newlines.
Other Flags
| Flag | Meaning |
|---|---|
-l | Count lines |
-w | Count words |
-c | Count characters (bytes) |
With Pipes
wc is very useful at the end of a pipeline:
grep "Linux" notes.txt | wc -l
Output:
1
This counts how many lines matched the pattern.
ls | wc -l
Counts the number of items in the current directory.
Your Task
Count the number of lines in notes.txt using wc -l.
Linux shell loading...
Loading...
Click "Run" to execute your code.