Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The sort Commands You Will Use Most

sort names.txt
sort -n numbers.txt
sort -u hosts.txt
sort | uniq -c | sort -rn | head

The first sorts alphabetically, the second by numeric value, and the third sorts and drops duplicate lines. The last line is a pattern rather than a command on its own: placed after another command, it counts how often each line occurs and shows the most frequent first.

Sort Numbers and Reverse

Without -n, sort compares text character by character, so 10 comes before 9. -n compares numbers. -r reverses any order:

sort -n sizes.txt
sort -rn sizes.txt

Sizes Like 4K, 120M, and 2G

sudo du -sh /var/* | sort -h
sudo du -sh /var/* | sort -rh | head

-h understands the suffixes printed by du -h and df -h, so 2G sorts after 900M. The second command lists the largest folders first; more on this is in how to check disk space in Linux.

Sort by a Column

sort -k 2 file.txt
sort -k 3,3n file.txt
sort -t: -k 3,3n /etc/passwd

-k 2 sorts by everything from the second field to the end of the line. -k 3,3 limits the key to the third field only, and the n after it makes that key numeric. Fields are separated by blanks unless you set another character with -t; the last example sorts user accounts by their ID. Several keys are allowed: sort -k 1,1 -k 2,2n sorts by the first field, then numerically by the second.

More Options

OptionWhat it does
-fIgnores case
-VSorts version numbers
-o fileWrites the result to a file
-cChecks if input is sorted

To sort a file in place, use sort -o list.txt list.txt. Never write sort list.txt > list.txt: the shell empties the file before sort reads it, and the content is lost.

Remove Duplicates

sort -u list.txt
sort list.txt | uniq
sort list.txt | uniq -d

For whole lines, sort -u and sort | uniq give the same result. uniq removes only repeated lines that are next to each other, which is why its input is sorted first. uniq -d prints only the lines that occur more than once, and uniq -u only those that occur once.

Count Repeats with uniq -c

sort list.txt | uniq -c | sort -rn

uniq -c prints each distinct line with the number of times it occurred. The second sort puts the highest counts first.

Top IP Addresses in an Access Log

awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10

awk prints the client address of each request, sort groups equal addresses together, uniq counts them, and the final sort with head shows the ten busiest clients. Replace $1 with $7 for the most requested paths, or $9 for status codes. The field numbers are explained in the awk command guide, and to watch the same log live, see tail -f.

Speed and Order on Large Files

The sort order depends on the locale: with a UTF-8 locale such as en_US.UTF-8, sort follows language rules, which can mix upper and lower case and give punctuation less weight. LC_ALL=C sort sorts by plain byte value, which is predictable and often faster on large logs. sort handles files larger than memory by writing temporary files; -T /path puts them on a disk with enough space.

Was this answer helpful?
Loading...