wc
wc is like a counter that tells you how many lines, words or letters are in some text. It is handy at the end of a chain to see how much made it through.
In this page:
Syntax
wc [-l | -w | -c] file_name
wc
wc counts lines (-l), words (-w), and characters/bytes (-c) in its input. It's often placed at the end of a pipeline to summarize how much data made it through previous filtering steps.
Running it with no flags prints all three counts together.
Note:
command | wc -l is the standard idiom for counting how many lines a command produced.Example: wc
#!/bin/bash
printf "one\ntwo\nthree\n" > words.txt
echo "Lines: $(wc -l < words.txt)"
echo "Words: $(wc -w < words.txt)"
echo "Characters: $(wc -c < words.txt)"
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Using
wc -land expecting the last line to count without a trailing newline. - Reading
wc -l fileoutput as only a number, when it includes the file name. - Confusing
-c(bytes) and-m(characters).
🔒
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: