Difficulty: Easy
Correct Answer: ls | wc -l
Explanation:
Introduction / Context:
Pipelines combine simple UNIX tools to accomplish useful tasks. A common need is to count how many items a command lists. The wc (word count) utility can report lines, words, and bytes for its input, making it perfect to summarize ls output.
Given Data / Assumptions:
Concept / Approach:
wc -l counts lines of input. Piping ls to wc -l yields the number of lines produced by ls, which typically corresponds to the number of visible directory entries. If your environment formats ls in multiple columns, use ls -1 to force one per line before piping.
Step-by-Step Solution:
Verification / Alternative check:
Compare ls -1 output visually and ensure the number of lines matches wc -l. For robust scripting, prefer find with explicit type filters and null-delimited results when filenames include newlines.
Why Other Options Are Wrong:
a: wc -c reports byte count, not lines.
b: wc -w reports word count, not reliable for filenames with spaces.
d: wc without options prints lines, words, and bytes; it does not directly return just the line count.
e: Not applicable because ls | wc -l is correct.
Common Pitfalls:
Relying on ls default column formatting; to be safe, use -1. Also note that ls counts directories and files together; use find to tailor results precisely.
Final Answer:
ls | wc -l
Discussion & Comments