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 –version displays version information of sort command.
sort --version
Mechanism of sort command
- Lines starting with a number will be sorted in ascending order. Lines starting with a number go before the lines beginning with a letter.
- 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
- Sorting text file where all the lines start with a capital letter
sort abc.txt
2. Sorting text file where lines start with a capital letter or small letter
sort UpperLower.txt
3. storing output in new text file
sort abc.txt > output.txt
4. alternatively it can also be used to storing output in new text file
sort -o newoutput.txt abc.txt
5. display sort output in reverse order
sort -r abc.txt
sort 123.txt
sort 123abc.txt
8. line starting with number and letter
sort -n 123abc.txt
9. reverse sort output for number
sort -nr 123.txt
sort -g 123abc.txt
11. sort according to first column
sort -k1 pink.txt
12. sort according to second column
sort -k2 pink.txt
sort -M calender.txt
14. check for sorted input; do not sort
sort -c sortc.txt
sort -u sortu.txt
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 |