CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File Viewing & Editing / wc

wc

Count lines, words and bytes

Beginneressentialpipescounting

wc counts lines, words, characters and bytes in files or in whatever you pipe into it. wc -l at the end of a pipeline is the standard way to answer 'how many?': errors in a log, files in a directory, connections, users, rows in a CSV.

Syntaxwc [-l] [-w] [-c] [-m] [-L] [FILE]...

Watch it run

You want to know how big a log is in lines, and compare files at a glance.

devops@devops-lab: /var/log/app — zsh — 96×28
➜ /var/log/app
Step 1 of 5
wc app.log

With no option, wc prints lines, words and bytes, then the name.

Anatomy of a command

$
commandgrep

Select the lines you want to count.

How it works

wc reads its input in large blocks and counts as it goes; nothing is kept in memory, so it handles files of any size. -l counts newline bytes (coreutils uses a vectorised fast path for this, so wc -l runs at disk speed); a final line without a trailing newline is therefore not counted. -c counts bytes, and for a regular file with -c alone wc can simply take the size from fstat without reading. -w counts words, sequences of non-whitespace characters. -m counts characters, which depends on the locale: in UTF-8 'é' is one character but two bytes. With several files wc prints a line per file and a total, padding the numbers into columns; when it reads standard input it prints no file name, which is why wc -l < file is the idiom for getting a bare number in scripts. Output order is always lines, words, characters, bytes, whatever order you give the options.

Options you'll actually use

-lCount lines (newline characters)
-wCount words (whitespace-separated)
-cCount bytes
-mCount characters (multibyte-aware, locale-dependent)
-LLength of the longest line
< fileRead stdin: print the number only, no file name
--files0-from=FRead NUL-separated file names (from find -print0)

Common mistakes

ss -tn | wc -l or ps aux | wc -l as the count

Both commands print a header line, so the count is off by one.

ss -tnH | wc -l, ps aux --no-headers | wc -l, or tail -n +2 first
echo "$var" | wc -c to measure a string

echo appends a newline, so the result is one too high (and wc -c counts bytes, not characters).

printf '%s' "$var" | wc -m, or ${#var} in bash
COUNT=$(wc -l file) and then using $COUNT in arithmetic

With a file argument wc prints '120 file', so $COUNT contains the file name too.

COUNT=$(wc -l < file)
find . -name '*.log' | wc -l with file names that contain newlines

Each newline in a name is counted as an extra file.

find . -name '*.log' -printf '.' | wc -c

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    How many lines are in ~/logs/access.log?

  2. 2

    Count the ERROR lines in ~/logs/app.log, printing only the number.

  3. 3

    Count the data rows (excluding the header) in ~/data/orders.csv and store the number in a variable.

  4. 4

    Show the line count of every file in ~/logs plus a total.

Related commands

grepSearch text for lines that match a patternheadShow the first lines or bytes of a filetailShow the last lines of a file and follow it livesortSort lines: alphabetically, numerically, by columnuniqCollapse and count repeated adjacent lines
PreviousnanoNextdiff
Linux / File Viewing & Editing / wc

wc

Count lines, words and bytes

Beginneressentialpipescounting

wc counts lines, words, characters and bytes in files or in whatever you pipe into it. wc -l at the end of a pipeline is the standard way to answer 'how many?': errors in a log, files in a directory, connections, users, rows in a CSV.

Syntaxwc [-l] [-w] [-c] [-m] [-L] [FILE]...

Watch it run

You want to know how big a log is in lines, and compare files at a glance.

devops@devops-lab: /var/log/app — zsh — 96×28
➜ /var/log/app
Step 1 of 5
wc app.log

With no option, wc prints lines, words and bytes, then the name.

Anatomy of a command

$
commandgrep

Select the lines you want to count.

How it works

wc reads its input in large blocks and counts as it goes; nothing is kept in memory, so it handles files of any size. -l counts newline bytes (coreutils uses a vectorised fast path for this, so wc -l runs at disk speed); a final line without a trailing newline is therefore not counted. -c counts bytes, and for a regular file with -c alone wc can simply take the size from fstat without reading. -w counts words, sequences of non-whitespace characters. -m counts characters, which depends on the locale: in UTF-8 'é' is one character but two bytes. With several files wc prints a line per file and a total, padding the numbers into columns; when it reads standard input it prints no file name, which is why wc -l < file is the idiom for getting a bare number in scripts. Output order is always lines, words, characters, bytes, whatever order you give the options.

Options you'll actually use

-lCount lines (newline characters)
-wCount words (whitespace-separated)
-cCount bytes
-mCount characters (multibyte-aware, locale-dependent)
-LLength of the longest line
< fileRead stdin: print the number only, no file name
--files0-from=FRead NUL-separated file names (from find -print0)

Common mistakes

ss -tn | wc -l or ps aux | wc -l as the count

Both commands print a header line, so the count is off by one.

ss -tnH | wc -l, ps aux --no-headers | wc -l, or tail -n +2 first
echo "$var" | wc -c to measure a string

echo appends a newline, so the result is one too high (and wc -c counts bytes, not characters).

printf '%s' "$var" | wc -m, or ${#var} in bash
COUNT=$(wc -l file) and then using $COUNT in arithmetic

With a file argument wc prints '120 file', so $COUNT contains the file name too.

COUNT=$(wc -l < file)
find . -name '*.log' | wc -l with file names that contain newlines

Each newline in a name is counted as an extra file.

find . -name '*.log' -printf '.' | wc -c

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    How many lines are in ~/logs/access.log?

  2. 2

    Count the ERROR lines in ~/logs/app.log, printing only the number.

  3. 3

    Count the data rows (excluding the header) in ~/data/orders.csv and store the number in a variable.

  4. 4

    Show the line count of every file in ~/logs plus a total.

Related commands

grepSearch text for lines that match a patternheadShow the first lines or bytes of a filetailShow the last lines of a file and follow it livesortSort lines: alphabetically, numerically, by columnuniqCollapse and count repeated adjacent lines
PreviousnanoNextdiff