Sort Command - Sort Text / Output on the Linux Command Line
From sorting log file entries to viewing directories ordered by size, the sort command is certainly among the GNU Core Utilities most versatile members. For professional admins and home users alike, sort is a daily tool.
This article will examine some common options and use-cases for this ever-flexible command. We will also take a look at the ability of sort to receive the output of a prior command and render it according to our needs.
Table of Contents
Basic Usage of the Sort Command
In its purest form and without optional arguments, sort will take lines of characters, typically from a file, and order them alphanumerically:
[email protected]:~$ cat sort.txt Arch Debian RHEL CentOS OpenSUSE Ubuntu Mint MX Fedora Puppy Bunsen
[email protected]:~$ sort sort.txt Arch Bunsen CentOS Debian Fedora Mint MX OpenSUSE Puppy RHEL Ubuntu
NOTE: We use the cat command to show the file contents before being sorted.
Each line is split separately, and blank spaces are interpreted by sort as a field separator. The internal logic of sort places numbers before letters and lower-case before upper-case.
[email protected]:~$ cat sort.txt Arch Debian RHEL CentOS OpenSUSE Ubuntu debian Mint MX Fedora Puppy Bunsen 1 0
[email protected]:~$ sort sort.txt 0 1 Arch Bunsen CentOS debian Debian Fedora Mint MX OpenSUSE Puppy RHEL Ubuntu
Sort Command Options
Reverse Sort Order
One of the first arguments a user will come across is -r, which reverses the sort order. Continuing with our previous example list:
[email protected]:~$ cat sort.txt Arch Debian RHEL CentOS OpenSUSE Ubuntu Mint MX Fedora Puppy Bunsen 1 0
[email protected]:~$ sort -r sort.txt Ubuntu RHEL Puppy OpenSUSE MX Mint Fedora Debian CentOS Bunsen Arch 1 0
The -r (--reverse) option can be used in conjunction with other sort options.
Another popular option for sort is -d for the dictionary order. The thorough experts at Gnu.org describe this as “phone directory order: ignore all characters except letters, digits and blanks when sorting.” Unless changed, the sort method assigns letters and digits an ASCII value while spaces or tabs are blank.
“The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional sort order that use native byte values”Sort Man Page
[email protected]:~$ cat sort.txt Arch Debian RHEL CentOS OpenSUSE Ubuntu Mint MX Fedora Puppy Bunsen 1 0 ############## *************
[email protected]:~$ sort -d sort.txt ************* ############## 0 1 Arch Bunsen CentOS Debian Fedora Mint MX OpenSUSE Puppy RHEL Ubuntu
Case Insensitive Sorting
There are any number of instances where the capitalization of a word or string is irrelevant to the user and we just need the letter’s order.
[email protected]:~$ cat sort.txt Arch Debian RHEL CentOS OpenSUSE Ubuntu Mint MX Fedora Puppy Bunsen ARCH DEBIAN rhel mx
[email protected]:~$ sort -f sort.txt Arch ARCH Bunsen CentOS Debian DEBIAN Fedora Mint mx MX OpenSUSE Puppy rhel RHEL Ubuntu
Ignore Duplicates / Sorting Unique
There will be scenarios where duplicated data is clouding your vision. Cut through that clutter by only displaying the first of however many repeating entries; sort uses the -u argument for this purpose.
[email protected]:~$ cat sort.txt Arch ARCH ARCH Bunsen CentOS Debian DEBIAN Fedora Mint mx mx MX MX OpenSUSE Puppy rhel rhel RHEL RHEL Ubuntu
[email protected]:~$ sort -u sort.txt Arch ARCH Bunsen CentOS Debian DEBIAN Fedora Mint mx MX OpenSUSE Puppy rhel RHEL Ubuntu
Note: sort -u and sort | uniq are equivalent, when you invoke sort | uniq option-free. Read about the uniq command here.
Sort in Numeric Order
The -n option is useful for any numerical strings. Here we are manipulating a simple list of single and double digit integers:
[email protected]:~$ cat nums.txt 0 1 2 15 3 4 5 14 6 7 8 9 0 13 11 12 10
[email protected]:~$ sort -n nums.txt 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Multiple Sort Columns
Conceptually, the “key” in the sort command functions very similarly to the way that a database table’s primary key works. We need a field to be the deciding factor upon which we sort. Ordinarily, the first character of the line is the “key”. In other words, the key character determines the sequence order.
We may need to skip certain fields, or certain portions of lines. We can get more granular with our sorting by changing the “key” with the -k option.
[email protected]:~$ ls -l /home/mtatum/ | sort -rk9 drwxr-xr-x 2 mtatum mtatum 4096 Dec 26 14:44 Videos drwxr-xr-x 2 mtatum mtatum 4096 Dec 26 14:44 Templates drwxr-xr-x 2 mtatum mtatum 4096 Dec 26 14:44 Public drwxr-xr-x 4 mtatum mtatum 4096 Jan 24 18:10 Pictures drwxr-xr-x 2 mtatum mtatum 4096 Dec 26 14:44 Music drwxr-xr-x 2 mtatum mtatum 4096 Feb 9 12:58 Downloads drwxr-xr-x 5 mtatum mtatum 4096 Jan 24 18:10 Documents drwxr-xr-x 2 mtatum mtatum 4096 Dec 27 16:55 Desktop drwxrwxrwx 14 mtatum mtatum 4096 Mar 6 2016 Audiobooks total 36
Here we’re once again leveraging the useful piping capability of sort; we’ve sorted the list output of the user’s home directory. This example is using the 9th column’s friendly folder name as the “key”, as well as the -r option to invert the alphabetical order.
Checking Sort Version
Though seemingly counter-intuitive, one of the most important of sort might not include sorting of any kind. You can display the package information from your local system by adding the --version option to sort. If there were ever to be a security flaw discovered in the sort package, it would be imperative to check your vulnerability as soon as possible. Once patched and cleanly secured, the sort --version command would display the new hardened version number, thereby confirming your safety.
[email protected]:~$ sort --version sort (GNU coreutils) 8.28 Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Mike Haertel and Paul Eggert.
Sorting by File/Folder Size
Users may need to determine which directories are utilizing the most space on their filesystem. In both home or office environments managing the hard drive usage is essential.
[email protected]:~$ ls && du -sh * | sort -h Audiobooks Desktop Documents Downloads Music Pictures Public Templates Videos 4.0K Desktop 4.0K Music 4.0K Public 4.0K Templates 4.0K Videos 1.2G Audiobooks 2.7G Downloads 7.6G Documents 13G Pictures
This may seem to be a more complex command but it’s actually very straightforward. From the user’s home directory, we are listing the contents and then sorting the directories by human-readable size. The du command’s output is piped into the sort command. We append this compound command to the original ls command (with &&) for the time-savings and display convenience.
For more information on logical control operators (&& or ||) read "Bash - Using Control Operators".
Sorting a Log File
Another useful scenario is examining when and whom has logged into your system:
[email protected]:/var/log$ last | sort -bd mtatum tty7 :0 Mon Feb 3 15:50 - 15:00 (3+23:10) mtatum tty7 :0 Sat Feb 8 09:53 gone - no logout reboot system boot 5.3.0-28-generic Mon Feb 3 15:49 - 15:00 (3+23:10) reboot system boot 5.3.0-28-generic Sat Feb 8 09:53 still running wtmp begins Sun Feb 2 20:02:30 2020
Here we have taken the output of the last command and piped it into sort. We want to ignore leading space and use dictionary mode thus the -bd options are employed. In this case the list is relatively short but for highly used systems it can be useful to group the long list of logins by username.
Thank you for joining us. In this article we took an overview of the various applications of the sort command. We also walked through the very useful coupling of sort with other command’s piped outputs. We’ve only scratched the surface of the many ways in which sort can make your UNIX or UNIX-like system more efficient and enjoyable to use, so be sure to continue your learning with your own experimentation.
Resources and Links
This site uses Akismet to reduce spam. Learn how your comment data is processed.