Menu
Coddy logo textTech

Unique Command

Part of the Fundamentals section of Coddy's Terminal journey — lesson 33 of 82.

The uniq command filters out consecutive duplicate lines from a file, showing only unique lines.

We have duplicates.txt:

apple
banana
apple
cherry
banana
banana

Important: uniq only removes duplicates that are next to each other. This is why it is almost always used together with sort first:

sort duplicates.txt | uniq

The | symbol is called a pipe — it sends the output of one command as the input to another. This is a powerful pattern you will use often in the terminal.

Useful flags:

-c — prefix each line with the count of how many times it appears:

sort duplicates.txt | uniq -c
      2 apple
      3 banana
      1 cherry

-d — show only the lines that are duplicated:

sort duplicates.txt | uniq -d

-u — show only lines that appear exactly once:

sort duplicates.txt | uniq -u
challenge icon

Challenge

Beginner

Use sort and uniq together to display only the unique lines from duplicates.txt with no duplicates.

Hint: You need to sort the file first so duplicates are grouped together, then pipe | the result into uniq to remove them.

Cheat sheet

The uniq command filters out consecutive duplicate lines from a file. It only removes duplicates that are next to each other, so it's typically used with sort first:

sort duplicates.txt | uniq

The | symbol is a pipe — it sends the output of one command as input to another.

Useful flags:

-c — prefix each line with the count of occurrences:

sort duplicates.txt | uniq -c

-d — show only duplicated lines:

sort duplicates.txt | uniq -d

-u — show only lines that appear exactly once:

sort duplicates.txt | uniq -u

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals