sort command in Linux/Unix

sort command in Linux allows users to sort the content of text files. sort command prints the result in standard output and users can also write standard output to a file.

Syntax for sort command

sort [option] [filename]

To get help with sort command and display all options available with sort command use sort –help.

sort --help

sort --help
sort –version displays version information of sort command.

sort --version

sort --version

Mechanism of sort command

  1. Lines starting with a number will be sorted in ascending order. Lines starting with a number go before the lines beginning with a letter.
  2. Lines starting with letters will be sorted in ascending alphabetical order. Lines starting with a capital letter (A-Z) go before the lines starting with a small letter (a-z).

Using sort command

  1. Sorting text file where all the lines start with a capital letter
sort abc.txt

sort filename
2. Sorting text file where lines start with a capital letter or small letter

sort UpperLower.txt

sort UpperLowerCasefile
3. storing output in new text file

sort abc.txt > output.txt

sort filename outputfilename
4. alternatively it can also be used to storing output in new text file

sort -o newoutput.txt abc.txt

sort -o outputfile filename
5.  display sort output in reverse order

sort -r abc.txt

sort -r filename
6. line starting with number

sort 123.txt

sort filenameNumeric
7. h

sort 123abc.txt

sort filenameAlphaNumeric
8. line starting with number and letter

sort -n 123abc.txt


9. reverse sort output for number

sort -nr 123.txt

sort -nr filename
10. general numeric value

sort -g 123abc.txt

sort -g filename

11. sort according to first column

sort -k1 pink.txt

sort -k1 filename
12. sort according to second column

sort -k2 pink.txt

sort -k2 filename
13. sort calender

sort -M calender.txt

sort -M calenderfile
14. check for sorted input; do not sort

sort -c sortc.txt

sort -c filename
15. remove repeat

sort -u sortu.txt

sort -u filename

[insert page=’linux-commands-with-example’ display=’content’]

Scroll to Top