On Unix/Linux, which command counts the total number of lines, words, and bytes/characters in a file in one invocation?

Difficulty: Easy

Correct Answer: wc

Explanation:


Introduction / Context:
When auditing logs, measuring dataset sizes, or validating script output, a compact summary of lines, words, and bytes is helpful. Unix/Linux provides a standard utility that computes these metrics efficiently for one or many files and can be combined with pipelines for flexible analysis.


Given Data / Assumptions:

  • You have one or more text or binary files.
  • You need counts for lines, words, and bytes/characters.
  • POSIX shell tools are available.


Concept / Approach:
The 'wc' command (word count) reports three values by default: line count, word count, and byte count (often interpreted as characters for single-byte encodings). Options '-l', '-w', and '-c' restrict output to a specific metric. With multiple files, 'wc' prints per-file statistics and an aggregate total line at the end.


Step-by-Step Solution:

Basic use: wc filenameCount only lines: wc -l filenameCount only words: wc -w filenameCount only bytes: wc -c filename


Verification / Alternative check:
Cross-verify bytes with 'stat -c %s filename' (GNU) and lines with 'nl' or 'awk 'END{print NR}' filename'. The results should match for regular files.


Why Other Options Are Wrong:

  • countw / wcount / count p: Not standard Unix commands.
  • None of the above: Incorrect because 'wc' is correct.


Common Pitfalls:
Confusing bytes with characters in multibyte encodings, and forgetting that 'wc' on stdin (for example, 'wc < file') omits the filename in the output format.


Final Answer:
wc

Discussion & Comments

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