LESSON · 1 OF 6

cut, sort, and uniq

cut, sort, and uniq

Three commands that show up in almost every shell pipeline.

cut: pull columns out of structured text

bash
cut -d ':' -f 1 /etc/passwd       # just the usernames (field 1)
cut -d ':' -f 1,7 /etc/passwd     # username and login shell
cut -c 1-10 /etc/passwd           # first 10 characters of each line

-d sets the delimiter (colon, comma, tab, whatever), -f picks fields by number. -c selects character ranges directly.

sort: order lines

Make a small file to follow along (the numbers.txt and sizes.txt examples below are illustrative variants):

bash
printf 'banana\napple\napple\ncherry\n' > names.txt
bash
sort names.txt                    # alphabetical
sort -r names.txt                 # reverse
sort -n numbers.txt               # numeric (10 comes after 2, not before)
sort -h sizes.txt                 # human-readable sizes (10K before 5M)
sort -u names.txt                 # sorted AND deduplicated in one step

You met sort -h in the disk-usage topic. The rest earn their keep almost daily.

uniq: remove consecutive duplicates

Important: uniq only removes adjacent duplicates. It's designed to run after sort.

bash
sort names.txt | uniq                    # dedupe
sort names.txt | uniq -c                 # dedupe AND count occurrences
sort names.txt | uniq -c | sort -rn      # top-N pattern, biggest first

That last line is one of the most useful DevOps idioms: count how often each unique value appears, then sort by count descending. Perfect for finding the top IPs in an access log, the most frequent error messages, or the busiest endpoints. You'll type it every week.

Spin up a fresh environment and practice live.
linux-devops-basic · fresh machine · ready in under a minute