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



Linux command with examples

A
adduser | addgroup | alias | anacron | apt | aptitude | arp | at | atq | atrm | awk
B
basename | banner | batch | bc | bg | bzip
C
cat | cal | cd | chgrp | chown | cksum | chmod | clear | cmp | comm | cp
D
date | dd | df | diff | dir | dmidecode | du
E
echo | eject | env | exit | expr
F
factor | find | free
G
grep | groups | gunzip | gzip
H
head | history | hostname | hostnamectl | htop | hwclock | hwinfo
I
id | ifconfig | ionice | iostat | ip | iptables | iw | iwlist
J
K
kill | kmod
L
last | less | ln | locate | login | lp | ls | lshw | lscpu | lsof | lsusb
M
man | mdsum | mkdir | more| mv
N
nano | nc | neofetch | netcat | netstat | nice | nmap | nproc
O
openssl
P
passwd | pidof | ping | pr | ps | pwd | pstree
Q
R
rdiff-backup | reboot | rename | rm | rmdir | rnmod
S
scp | shred | shutdown | sleep | sort | split | ssh | stat | su | sudo | sum
T
tac | tail | talk | tar | tee | time | tree | top | touch | tr
U
unalias | uname | uniq | unzip | uptime | users
V
vim | vi
W
w | wall | watch | wc | wget | whatis | whereis | which | who | whoami
X
xargs
Y
yes | youtube-dl
Z
zcmp | zdiff | zip | zz

Sharing is Caring
Scroll to Top