Linux command list

sort command: put lines in order

__ __ _ _____
\ \ / /__| |_|_ _|__ _ __ _ __ ___
\ \ /\ / / _ \ '_ \| |/ _ \ '__| '_ ` _ \
\ V V / __/ |_) | | __/ | | | | | |
\_/\_/ \___|_.__/|_|\___|_| |_| |_| |_
 
A sandbox for trying sort. Nothing here can touch your real files.
user@webterm:~/project$
 

sort reorders the lines of a file. Learn when you need `-n` for numbers, how `-u` drops duplicates, and how `-t` and `-k` pick a column in a CSV, by running the commands in a real terminal in your browser.

Updated: 2026-09-06

Syntax

sort [options] file...

It prints the lines in order. The file itself is left alone.

Try it first

$ cat names.txt
grace
ada
linus
ada
margaret
$ sort names.txt
ada
ada
grace
linus
margaret

The default is alphabetical order. -r reverses it.

$ sort -r names.txt
margaret
linus
grace
ada
ada

Sorting numbers

Hand sort a file of numbers and the order looks wrong.

$ sort sizes.txt
10
100
2
9

Compared as text one character at a time, 10 really does come before 2. -n compares them as numbers.

$ sort -n sizes.txt
2
9
10
100

Add -r for largest first.

$ sort -nr sizes.txt
100
10
9
2

Dropping duplicates

-u keeps one copy of each line.

$ sort -u names.txt
ada
grace
linus
margaret

Sorting by a column

-t sets the separator and -k picks the field. Ordering a CSV by name, the second column:

$ sort -t, -k2 users.csv
1,ada,web
3,grace,infra
2,linus,infra
id,name,team

The header is just another line to be sorted, which is why it moved to the bottom.

Counting, most common first

sort is at its best next to uniq -c.

$ cut -d' ' -f1 access.log | sort | uniq -c | sort -nr
      3 200
      2 404
      1 500

Read as three steps it is simple enough.

StepWhat it does
sortPuts identical values next to each other
uniq -cCounts neighbouring duplicates
sort -nrOrders by that count, largest first

uniq only ever looks at neighbouring lines, so the first sort is not optional.

When you actually reach for it

sort is what you type to see the biggest or most frequent thing first.

SituationWhat to type
Count status codes, most common firstcut -d' ' -f1 access.log | sort | uniq -c | sort -nr
Get a list with duplicates removedsort -u emails.txt
Order a CSV by one columnsort -t, -k2 users.csv
Keep the sorted resultsort names.txt > sorted.txt

Things that trip people up

Numbers need -n. Forget it while sorting file sizes or counts and 100 lands before 9.

The file is not modified.

$ sort names.txt > sorted.txt
$ cat sorted.txt
ada
ada
grace
linus
margaret

Write the output to another file when you want to keep it.

uniq needs a sort in front of it. uniq compares each line only with the one before it. Identical lines that are far apart stay separate until you sort them together.

Headers get sorted too. If the first line of a CSV has to stay put, take it off before handing the rest to sort.

Practise it hands-on

webterm.appthis site

  • Advanced Terminal Commands

    Learn commands for specific situations

    Try the tutorial

learn.webterm.appa separate site

>_WEBTERM LEARN

WebTerm Learn: from one command to actually using it

Commands stick when they show up in a real sequence of work, not one at a time. There is a course that builds them up in order.

See the course

Related commands

uniq / wc / grep / cut

Frequently asked questions

Why does 10 come before 2?
By default lines are compared character by character, so the 1 in 10 beats the 2. Add `-n` to compare them as numbers.
How do I drop duplicate lines?
`sort -u`. `uniq` can do it too, but `uniq` only looks at neighbouring lines, so the input has to be sorted first.
How do I sort a CSV by its second column?
`sort -t, -k2 users.csv`. `-t` sets the separator and `-k` picks the field.
Does sort change the file?
No. The result only goes to the screen, so write it somewhere if you want to keep it: `sort names.txt > sorted.txt`.
How do I count things and see the most common first?
`sort | uniq -c | sort -nr`. Group the lines, count each group, then order by that count.