Linux 'sort' command examples (Linux command line sorting)

Linux sort command FAQ: Can you share some examples of the Unix/Linux sort command?

As its name implies, the Unix/Linux sort command lets you sort text information. This tutorial shares several examples of the sort command.

Sorting `ls` command output

You can use the sort command to sort all kinds of output from other commands. For instance, here's an example where I sort the output of the ls -al command:

$ ls -al | sort -n -k5

This results in the following ls command sorted output, which as you can see, is a directory listing, sorted by filesize (the 5th column):

-rw-r--r--   1 al  al       0 Aug 17  2007 CreateAPodcast.idx
total 992
-rw-r--r--   1 al  al     240 Aug 17  2007 files
drwxr-xr-x  11 al  al     374 Jul  5 17:50 ..
-rw-r--r--   1 al  al     535 Aug 18  2007 CreateAPodcast.out
drwxr-xr-x  18 al  al     612 Aug 18  2007 images
drwxr-xr-x  20 al  al     680 Aug 27  2007 .
-rw-r--r--   1 al  al     978 Aug 18  2007 CreateAPodcast.toc
-rw-r--r--   1 al  al    1425 Aug 18  2007 CreateAPodcast.lof
drwxr-xr-x  50 al  al    1700 Aug 18  2007 CreateAPodcast
-rwxr-xr-x@  1 al  al    2716 Aug 18  2007 CreateAPodcast.tex
-rw-r--r--   1 al  al    4431 Aug 18  2007 CreateAPodcast.aux
-rw-r--r--@  1 al  al    9689 Aug 17  2007 CAPContent2.rga copy
-rw-r--r--   1 al  al   13564 Aug 18  2007 CreateAPodcast.log
-rw-r--r--@  1 al  al   14369 Aug 18  2007 CAPContent2.tex
-rw-r--r--@  1 al  al   14738 Aug 31  2007 CAPContent2.rga
-rw-r--r--@  1 al  al  310657 Aug 18  2007 create-a-podcast-2.mp3

The -n in my example means "sort numerically", and the -k5 option means to key off of column five. Like other Unix commands, these sort command options can be combined and shortened, like this:

$ ls -al | sort -nk5

which yields the same output.

(Note that the ls command now has a sort option; I just showed this as an example.)

Sorting output of the ‘ps’ command

From time to time you'll want to sort the output of the Linux ps command, and again here, the sort command can be your friend. You can just sort alphabetically by the first column (username):

$ ps auxw | sort

Or you can sort numerically by column two (the PID field):

$ ps auxw | sort -nk2

You can also reverse that sort with the -r option:

$ ps auxw | sort -rnk2

Sorting `wc` output

I just used this command to sort the output of a word count command:

$ wc -l *md | sort -n

That shows the longest files at the bottom of the output.

Sorting file contents with ‘cat’

You can also sort the contents of a file with the Linux sort command. Here's what a file named files looks like before I sort it:

$ cat files 

1-gb-startup.jpg
10-after-drum.jpg
2-use-for-buttons.jpg
3-after-media-button.jpg
4-after-loop-browser.jpg
5-after-jingles.jpg
6-after-male-voice.jpg
7-after-chipmunk.jpg
8-before-2nd-male-recording.jpg
9-after-2nd-male-recording.jpg

And here's the output when I run a simple sort command against it:

$ sort files 

1-gb-startup.jpg
10-after-drum.jpg
2-use-for-buttons.jpg
3-after-media-button.jpg
4-after-loop-browser.jpg
5-after-jingles.jpg
6-after-male-voice.jpg
7-after-chipmunk.jpg
8-before-2nd-male-recording.jpg
9-after-2nd-male-recording.jpg

It's very important to note that this command does not sort the actual file, it just displays the sorted output on your terminal. To have the sorted output to another file, you'd run a command like this:

$ sort files > files.sorted

which creates a new file named files.sorted, which contains the new, sorted output.

Linux `sort` help

The output from the sort --help command is pretty short, so I'll include it here:

$ sort --help

Usage: sort [OPTION]... [FILE]...
Write sorted concatenation of all FILE(s) to standard output.

Mandatory arguments to long options are mandatory for short options too.
Ordering options:

  -b, --ignore-leading-blanks  ignore leading blanks
  -d, --dictionary-order      consider only blanks and alphanumeric characters
  -f, --ignore-case           fold lower case to upper case characters
  -g, --general-numeric-sort  compare according to general numerical value
  -i, --ignore-nonprinting    consider only printable characters
  -M, --month-sort            compare (unknown) < `JAN' < ... < `DEC'
  -n, --numeric-sort          compare according to string numerical value
  -r, --reverse               reverse the result of comparisons

Other options:

  -c, --check               check whether input is sorted; do not sort
  -k, --key=POS1[,POS2]     start a key at POS1, end it at POS2 (origin 1)
  -m, --merge               merge already sorted files; do not sort
  -o, --output=FILE         write result to FILE instead of standard output
  -s, --stable              stabilize sort by disabling last-resort comparison
  -S, --buffer-size=SIZE    use SIZE for main memory buffer
  -t, --field-separator=SEP  use SEP instead of non-blank to blank transition
  -T, --temporary-directory=DIR  use DIR for temporaries, not $TMPDIR or /tmp;
                              multiple options specify multiple directories
  -u, --unique              with -c, check for strict ordering;
                              without -c, output only the first of an equal run
  -z, --zero-terminated     end lines with 0 byte, not newline
      --help     display this help and exit
      --version  output version information and exit

POS is F[.C][OPTS], where F is the field number and C the character position
in the field.  OPTS is one or more single-letter ordering options, which
override global ordering options for that key.  If no key is given, use the
entire line as the key.

SIZE may be followed by the following multiplicative suffixes:
% 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.

With no FILE, or when FILE is -, read standard input.

*** WARNING ***
The locale specified by the environment affects sort order.
Set LC_ALL=C to get the traditional sort order that uses
native byte values.

Report bugs to <bug-coreutils@gnu.org>.

More Linxu `sort` command information

If you have any sort commands you'd like to share, please add them to our comments section below. For more help, you can also type:

man sort

or

sort --help

on your Unix/Linux system.