[uniq] Count repeated lines with uniq -c

Created on November 30, 2026

Use uniq -c to group adjacent identical lines and print a count next to each one. Sort first if you need to count duplicates across all of the input.

printf '%s\n' apple banana apple banana banana orange | sort | uniq -c
      2 apple
      3 banana
      1 orange

The spaces align the counts and may vary between implementations. Add sort -nr to display the largest counts first.

printf '%s\n' apple banana apple banana banana orange | sort | uniq -c | sort -nr
      3 banana
      2 apple
      1 orange

Reference

Shell command uniq