Courseiva
GNU and Unix Commands →easyMultiple Select

LPIC-1 GNU and Unix Commands Practice Question

Which TWO of the following commands output the total number of lines in a file?

⚠ Common exam trap

Candidates often think `grep -c '.'` counts all lines, but it actually excludes empty lines, while `nl` outputs numbered lines rather than a single total count.

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

✓

sed -n '$='

Option D, `sed -n '$=',` is correct because sed's `=` command prints the current line number, and with the `$` address it prints only the line number of the last line, which equals the total number of lines in the file. Option E, `wc -l`, is correct because `wc` with the `-l` flag counts and outputs the number of newline characters, which is the standard way to report a file's total line count. Option A, `grep -c '.',` is wrong because it counts only lines containing at least one character (the `.` regex matches any single character), so it omits empty lines and does not give the true total. Option B, `nl`, is wrong because it numbers and prints every line of the file rather than outputting a single total count. Option C, `head -n 1`, is wrong because it outputs only the first line of the file, not the total number of lines.

Answer analysis

Option-by-option breakdown

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

  • ✗

    grep -c '.'

    Why it's wrong here

    grep -c '.' counts matching lines, but a line containing no characters (empty line) fails to match the dot, so blank lines are excluded and the total is understated. It is tempting because grep -c is a genuine line-counting idiom. wc -l counts every newline-terminated line regardless of content, giving the true total.

  • ✗

    nl

    Why it's wrong here

    nl numbers lines and writes them to standard output; it never emits a count. It is tempting because it processes the whole file line by line, so it looks like a counting tool. wc -l reads the file and prints the number of newline characters, which is the required total.

  • ✗

    head -n 1

    Why it's wrong here

    head -n 1 prints only the first line of the file, not a count of all lines. It is tempting because it is a line-oriented command and the -n flag takes a number, so it appears to relate to line totals. wc -l outputs the actual number of lines in the file.

  • ✓

    sed -n '$='

    Why this is correct

    The sed -n '$=' command suppresses default printing and, at the last line ($), executes the = command, which prints the current line number. For a file, that final number equals the total line count, satisfying the requirement without printing the lines themselves.

  • ✓

    wc -l

    Why this is correct

    wc -l counts newline characters in the input and reports that tally as the line count. This directly satisfies the requirement to output the total number of lines in a file, making it the standard utility for this task.

About these practice questions

One of 402 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.