Courseiva
Shells, Scripting and Data ManagementhardMultiple SelectObjective-mapped

LPIC-1 Shells, Scripting and Data Management Practice Question

Which TWO commands can be used to count the number of lines in a file named 'data.txt'?

⚠ Common exam trap

Test-takers frequently confuse `wc -c` (byte count) with `wc -l` (line count), or assume `grep -c '.*'` counts all lines without realizing it may miss empty lines or behave differently across grep implementations.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

wc -l data.txt

`wc -l` specifically counts the number of newline characters in the file, which corresponds to the number of lines. Option B is correct because `awk 'END{print NR}'` processes the file line by line, and the built-in variable `NR` holds the total number of records (lines) processed when the END block is executed, thus outputting the line count.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • wc -l data.txt

    Why this is correct

    wc -l counts line endings.

  • awk 'END{print NR}' data.txt

    Why this is correct

    NR holds the number of records processed.

  • cat data.txt | wc -c

    Why it's wrong here

    wc -c counts characters.

  • grep -c '.*' data.txt

    Why it's wrong here

    grep -c counts matching lines; '.*' matches all lines but if file empty, count is 0; not standard.

  • sed -n '$=' data.txt

    Why it's wrong here

    sed -n '$=' prints line number of last line, not count (same if file non-empty but fails for empty).

About these practice questions

One of 527 original LPIC-1 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This LPIC-1 practice question is part of Courseiva's free LPI certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the LPIC-1 exam.