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 | uniqThe | 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 -uChallenge
BeginnerUse sort and uniq together to display only the unique lines from duplicates.txt with no duplicates.
Hint: You need to
sortthe file first so duplicates are grouped together, then pipe|the result intouniqto 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 | uniqThe | 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 -uTry it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline