Command Line Tools

sort command allow you to sort the content of file. With sort command you can sort the contents in several ways. By default, the sort command sorts the contents in alphabetical order depending on the first letter in each line.

In this article we would cover following topic

  • Example of sort command
  • How to sort a file in alphabetical order in linux
  • How to sort by column in linux
  • How to sort in reverse order in linux
  • How to merge files with sort
  • How to sort files by size
  • List of options used with sort command

Example of sort command

Create a sample file with dummy names and age

$cat > test_file

sort create test file

How to sort a file in alphabetical order in linux

To sort this file alphabetically depending on name run following command

$sort test_file

sort default

How to sort by column in linux

To sort this file depending on age run following command

$sort -k2 test_file

-k2 is the option which refers to the second column. You can specify other column also. Suppose that file contain 8 columns and your desired column number is 6 than you should use -k6.

sort k option

How to sort in reverse order in linux

To sort in reverse order use -r option with sort command. You can also combine it with other options. To sort in reverse order of second column run following command

$sort -r -k2 test_file

sort k r option

How to merge files with sort

sort create two file

-m option allows us merge files in a single file.

sort m option

How to save sort output in file

By default sort command will print out on standard output. Nothing is going to write in file. To save output in file either use -o option or use redirect.

sort demo file

Use redirect method to save the output of sort command

sort redirect

Use -o option to save the output of sort command in file

sort o option

How to sort number in linux

Use -n option to sort based on number. Create a simple file with numbers and use default sort order.

It is sorted alphabetically.

sort number file

To sort this based on number use -n option

sort n option

How to sort files by size

You can sort files by size with use of sort command. ls command is used to list the contents of directory.

sort ls command

Use sort command with ls command to sort the files by size. As you can see in above image that size have column no 5. To sort the output of ls command based on size run following command

$sort ls -l | sort -k5

sort by size

List of options used with sort command

-r Sorts in reverse order
-s Stabilize sort by disabling last-resort comparison
-t Use SEP instead of non-blank to blank transition
-u If line is duplicated only display once
-b Ignores blank spaces at beginning of the line.
-c Check whether input is sorted or not.
-d Use dictionary sort order and ignores the punctuation.
-f Ignores caps
-k Start a key at POS1, end it at POS2 (origin 1)
-m Merges two or more input files into one file.
-M Treats the first three letters in the line as a month (such as jun.)
-n Sorts by the beginning of the number at the beginning of the line.
-o Write result to FILE instead of standard output
-o outputfile Save the sorted output to a file.

We have listed most frequently options used with sort command. To get a full list of all options with details read man page of sort command

$man sort