Counting lines, words, and characters: Which UNIX command reports the number of lines, words, and bytes/characters in a file?

Difficulty: Easy

Correct Answer: wc

Explanation:


Introduction / Context:
Quickly summarizing the size of text files is a common need for developers, data engineers, and sysadmins. UNIX provides a compact utility for this: wc (word count). It can be combined with options and pipelines to provide precise metrics in scripts and investigations.



Given Data / Assumptions:

  • We need counts for lines, words, and characters/bytes.
  • We are using standard POSIX-style utilities.
  • File encoding and locale may affect the character interpretation, but wc remains the standard tool.


Concept / Approach:

wc reports counts; options like -l, -w, -c (or -m) select lines, words, and bytes/characters respectively. Without options, wc prints all three counts followed by the filename. It is often used in shell pipelines to measure the output of other commands.



Step-by-Step Solution:

Run wc filename to see lines, words, bytes and the filename.Use wc -l filename for just lines; wc -w for words; wc -c for bytes; wc -m for characters on some systems.Pipe output from other commands: cat file | wc -lCombine with find and xargs to summarize multiple files.Redirect output to logs for auditing.


Verification / Alternative check:

Spot-check by manually counting a small file’s lines and words to confirm wc behavior. Use hexdump or od to explore byte counts for encodings.



Why Other Options Are Wrong:

a, c, d: Not standard UNIX utilities.

e: Not applicable because wc is correct.



Common Pitfalls:

Confusing bytes (-c) with characters (-m) in multibyte locales; forgetting to quote file patterns; relying on wc behavior across different UNIX variants without checking man pages.



Final Answer:

wc

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion