Which exact wc (word count) option counts only the number of bytes/characters in a file on Unix/Linux?

Difficulty: Easy

Correct Answer: wc -c

Explanation:


Introduction / Context:
The wc utility summarizes textual statistics and is commonly used in scripts and quick checks. Understanding its switches helps you retrieve only the metric you need—lines, words, or bytes/characters—without extra parsing.


Given Data / Assumptions:

  • You need the size as counted by bytes/characters.
  • You are operating on a regular text or binary file.
  • Locale and encoding may affect the interpretation of 'character'; wc -c counts bytes.


Concept / Approach:
wc -c reports the number of bytes in the input. On many systems this corresponds to character count for single-byte encodings. Other key options include -l for lines and -w for words. There is no standard '-r' or '- 1' option; those are distractors. Using the correct flag prevents confusion and enables reliable scripting logic.


Step-by-Step Solution:

Run: wc -c file.txtCapture output like: '12345 file.txt'To get only the number, use command substitution: wc -c < file.txtCombine with cut/awk if you provided a filename and need to strip it.


Verification / Alternative check:
Compare with stat -c %s file.txt (GNU) which reports size in bytes. The values should match for regular files not involving transformations like newline conversions.


Why Other Options Are Wrong:

  • wc -w: Counts words, not bytes.
  • wc -r: Not a standard option.
  • wc - 1: Not valid syntax; possibly a mistaken -l (lines).
  • None of the above: Incorrect because -c is the correct flag.


Common Pitfalls:
Confusing bytes with characters in multibyte encodings; for true character counts, use locale-aware tools like awk or iconv in combination with wc where appropriate.


Final Answer:
wc -c

Discussion & Comments

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