Sorting files

The command sort <filenames> sorts text files alphabetically by line. Sort considers each line as a record with space-separated fields. By default the first field is used to sort the file. The sort command has many options: sorting numerically, sorting in reverse order, sorting on fields within the line etc. Please check the man page using the command man sort for more options.

Here are some common options:

sort -r file1          reverse normal order
sort -n                sort in numeric order
sort -rn               sort in reverse numeric order
sort -f                fold upper and lower case together
sort -k 3,3            sort on the third field

For an example usage, see below:

[alice@onyx ~]$ cat listing 
1040 1680x1050.jpg
   4 log1
   4 log2
  16 logoCOENCS.png
1112 metageek_splash_screen.jpg
   0 testfile1
   0 tt

[alice@onyx ~]$ sort -n -k 1,1 listing 
   0 testfile1
   0 tt
   4 log1
   4 log2
  16 logoCOENCS.png
1040 1680x1050.jpg
1112 metageek_splash_screen.jpg

[alice@onyx ~]$