LPIC-1 uniq Practice Question
Which command combination will display a sorted list of unique lines from a file?
⚠ Common exam trap
The trap is assuming that `uniq` removes all duplicates. In fact, `uniq` only removes adjacent duplicates. Sorting before `uniq` (option C) is necessary to guarantee complete deduplication. Candidates may incorrectly think option A works in all cases, or that option D is correct.
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
✓
cat file | sort | uniq
(cat file | sort | uniq) is the only combination that always produces a sorted list of unique lines. The `sort` command first sorts all lines, making duplicates adjacent, then `uniq` removes those adjacent duplicates, guaranteeing a sorted unique output. Option A (cat file | uniq | sort) may omit non-adjacent duplicates, so it only works if the file is already sorted or duplicates happen to be adjacent. Option B (sort -u file) is a single command that also produces a sorted unique list, but the question specifically asks for a 'command combination' (i.e., a pipeline of multiple commands), so B is not a valid answer. Option D (Both A and C) is incorrect because A does not always work.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
cat file | uniq | sort
Why it's wrong here
Incorrect. This pipeline applies `uniq` before `sort`, so only adjacent duplicates are removed. If the file is not already sorted, non-adjacent duplicates remain, and the final output may contain duplicates.
- ✗
sort -u file
Why it's wrong here
Incorrect. `sort -u` does produce a sorted unique list, but it is a single command, not a combination of multiple commands. The question asks for a 'command combination', implying a pipeline.
- ✓
cat file | sort | uniq
Why this is correct
Correct. By sorting first (`sort`), all duplicates become adjacent, then `uniq` removes them. This guarantees a sorted list of unique lines regardless of the file's initial order.
- ✗
Both A and C
Why it's wrong here
Does not always work, so stating that both A and C are correct is false.
Go deeper
Related to this question
About these practice questions
This LPIC-1 question is part of Courseiva's 527-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
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.