Viewing files
wc
Count lines, words, characters and bytes in files or standard input.
wc [OPTION...] [FILE...]
wc (word count) prints, for each file, the number of newline characters, words and bytes, in
that order, followed by the file name. Options pick which counts you want: -l lines, -w
words, -c bytes, -m characters, -L the length of the longest line.
By far the most common use is wc -l at the end of a pipeline, to answer "how many?": how many
matches, how many files, how many unique visitors. Two details matter in practice: -l counts
newline characters, so a last line without one is not counted; and when you read from standard
input (wc -l < file) there is no file name in the output, which is what you want in a script.
Examples
Lines, words and bytes of a file
wc todo.txt
5 12 62 todo.txt
todo.txt has 5 lines, 12 words and 62 bytes. The columns are always in that order.
Count lines
wc -l access.log
20 access.log
-l prints only the line count: 20 requests in the log.
Get just the number, for a script
count=$(wc -l < todo.txt)
echo "There are $count items to do"
There are 5 items to do
Reading from standard input with < means wc has no file name to print, so the
substitution captures only the number.
Several files with a total
wc -l logs/*
12 logs/app.log 3 logs/app.log.1 2 logs/error.log 17 total
Each file gets its own line and wc adds a total line at the end.
Count the results of a pipeline
grep -c . access.log; grep ' 404 ' access.log | wc -l
20 4
grep ' 404 ' | wc -l counts the not-found requests: 4. For a single file grep -c gives
the same answer without the extra process, as the first count (all 20 non-empty lines) shows.
Count distinct values
cut -d ' ' -f 1 access.log | sort -u | wc -l
6
Extract the client IP of every request, keep each one once, and count them, giving 6 distinct clients.
Count words
wc -w data/poem.txt
16 data/poem.txt
A word is any run of characters separated by whitespace, so dog. and sleeps; count as one word each.
Characters versus bytes
printf 'café\n' | wc -m -c
5 6
-m counts characters (5: c a f é and the newline) and -c counts bytes (6), because é
takes two bytes in UTF-8. The output order is fixed, characters before bytes, whatever order
you give the options in.
A last line without a newline is not counted
printf 'one\ntwo\nthree' | wc -l
2
There are three lines of text but only two newline characters, and -l counts newlines, so
the answer is 2. Files written by some editors and by printf without \n behave like this.
Length of the longest line
wc -L access.log
144 access.log
-L reports the display width of the longest line, 144 columns here: useful before choosing a terminal width or a column limit.
Count lines in files found by find
find src -name '*.py' -print0 | wc -l --files0-from=-
3 src/lib/helpers.py 17 src/app.py 20 total
--files0-from=- reads NUL-separated file names from standard input, so names with spaces
or newlines are safe and there is no limit on how many files find returns.
Common pitfalls
wc -l fileprints the file name after the number. Usewc -l < fileto get only the number instead of piping throughawk '{print $1}'.BSD/macOS
wcpads the number with leading spaces even when reading standard input, so[ "$(wc -l < f)" = 5 ]fails there. Compare numerically with-eq, or strip spaces with$(( $(wc -l < f) )).wc -lcounts newline characters: a file whose last line has no newline reports one line fewer than you see.ls | wc -lmiscounts file names that contain newlines;find . -maxdepth 1 -printf . | wc -ccounts entries safely.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
--files0-fromand--totalare GNU options. BSD/macOSwchas-l,-w,-cand-m; for anything else check your system's man page.macOS
wcright-aligns numbers in a wide field even for one count read from standard input; GNU prints the bare number.
Options worth knowing
A selection, described in our own words. For the full list, see wc(1) or wc --help.
| Option | What it does |
|---|---|
-l, --lines | Print the number of newline characters. |
-w, --words | Print the number of whitespace-separated words. |
-c, --bytes | Print the number of bytes. |
-m, --chars | Print the number of characters, which differs from bytes for multi-byte text. |
-L, --max-line-length | Print the display width of the longest line. |
--files0-from FILE | Read NUL-terminated file names from FILE (- for standard input). |
--total[=WHEN] | When to print the total line: auto, always, only or never. |
Related
grepPrint the lines of files (or standard input) that match a pattern.sortSort lines of text alphabetically, numerically, by version or by chosen fields; also merge and de-duplicate.uniqCollapse or report repeated adjacent lines; with sort, count how often each line occurs.cutPrint selected fields, characters or bytes from each line, such as columns of a CSV or TSV file.nlPrint files with line numbers added, with control over which lines get numbered and how.- Pipes and pipelinesHow cmd1 | cmd2 connects processes, which exit status a pipeline returns, PIPESTATUS, pipefail, SIGPIPE, and why variables set in a piped loop vanish.
- Command substitutionCapturing a command's output with $(...), what happens to trailing newlines and stderr, how to check the exit status, and the local trap that hides failures.