uniq command (Linux / bash / sh / zsh)
What it does
Report or remove adjacent duplicate lines. When people search for the uniq command linux duplicates, they usually want uniq's everyday flags, safe examples, and how it differs from related tools. Below you'll find syntax, options, and copyable examples for Bash/sh/zsh.
Report or remove adjacent duplicate lines.
Syntax
uniq [OPTION]... [INPUT [OUTPUT]]
Common options
| Flag / option | Meaning |
|---|---|
| -c | Prefix lines with occurrence counts. |
| -d | Print only repeated groups. |
| -u | Print only nonrepeated groups. |
| -i | Ignore case when comparing. |
Practical examples
Remove duplicates
sort names.txt | uniq
Collapses equal adjacent names.
Count values
sort events.txt | uniq -c
Counts each grouped event line.
Repeated only
sort ids.txt | uniq -d
Prints IDs appearing more than once.
Common mistakes
- Running uniq on unsorted data and expecting all duplicates to disappear.
- Confusing `-u` unique-only output with normal deduplication.
Related commands
- sortOrder text lines using lexical, numeric, or keyed comparisons.
- wcCount lines, words, bytes, or characters in input.
- grepSearch text for lines matching a pattern.
- Where-ObjectFilter pipeline objects by property values or a script block predicate.
- catConcatenate files and write their contents to standard output.
- Get-ContentRead file content or provider data as a stream of objects.
- echoWrite arguments and shell-expanded values to standard output.
- echoDisplay text or control command echoing in a batch script.
FAQ
What does uniq do in Linux?
Report or remove adjacent duplicate lines.
How do I use uniq from the terminal?
Start with `uniq [OPTION]... [INPUT [OUTPUT]]`, then choose only the options needed for the task.
What is a practical uniq example?
Try `sort names.txt | uniq` — Collapses equal adjacent names.
Can I use uniq in a shell script?
Yes. uniq works in Bash, sh, and Zsh, but scripts should handle quoting, errors, and command output explicitly.
Browse the full Linux and Windows command reference to search more bash, CMD, PowerShell, Conda, and GitHub CLI commands.