The function of the sort command is to sort by line
Usage:
sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F
Options:
Sort class options:
-b. -- ignore leading blanks -- ignore spaces at the beginning of a line
-d. -- dictionary order only handles spaces, English letters and numbers
-f. -- ignore case -- lowercase letters are regarded as uppercase letters
-g. -- General numeric sort
-i. -- ignore nonprinting only consider printable characters
-M. -- month sort sort by month (other < 'Jan' <... < 'Dec')
-h. -- human numeric sort , sort by digital display with better readability (1K, 2G)
-n. -- numeric sort -- compare according to the number in the string
-R. -- random sort , sort randomly, but group the same keys
--Random source = FILE get random bytes from FILE
-r. -- reverse sort
--sort=WORD sort according to WORD. The options are: - g|h|M|n|R|V
-5. -- version sort -- sort by version number
Other options:
--Batch size = NMERGE = merge NMERGE input at most at one time, and use more temporary files
-c. -- check, -- check = diagnose first - check whether to sort
-c. -- check = quiet, -- check = silent - similar to - c, no error is output
--Compress program = PROG þ use PROG compression, and use PROG -d decompression
--The debug comment is used to sort the portion of the line and warn against suspicious use of stderr
--Reads the name from the specified file F = 0 from the specified file
-k. -- key = KEYDEF , sort by key, and the position and type are specified by KEYDEF
-m. -- merge , merge ordered files
-o. -- output = FILE outputs the sorting result to FILE
-s. -- stable comparison
-S. -- buffer SIZE = SIZE use SIZE as the main buffer SIZE
-t. -- field separator = SEP specifies SEP as the separator
-T. -- temporary directory = DIR use DIR as temporary file
--parallel=N ; set the number of sorts running at the same time to N
-u. -- unique , weight removal
-z. -- zero terminated line separator is empty, not newline
--help documentation
--version information
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] indicates the start and stop positions, where F is the field number and C is the character position in the field; Both start from # 1, and the stop position defaults to the end of the line. If - t and - B are invalid, the characters in the field are counted from the beginning of the previous space. OPTS is one or more single alphabetic sorting options [bdfgiMhnRrV], which overrides the global sorting options of the key. If no key is given, the entire line is used as the key. Use -- debug to diagnose incorrect key usage. SIZE can be suffixed:% b K M G T P E Z Y
Use example:
Use the du command to view the file under / usr/share, and take the first 10 results:
[root@server ~]# du -s /usr/share/* | head 12 /usr/share/aclocal 1332 /usr/share/alsa 648 /usr/share/anaconda 36 /usr/share/applications 8 /usr/share/augeas 440 /usr/share/authconfig 84 /usr/share/awk 18832 /usr/share/backgrounds 552 /usr/share/bash-completion 40 /usr/share/centos-logos
Sort using sort:
# In reverse numerical order (from large to small) [root@server ~]# du -s /usr/share/* | sort -nr | head 105112 /usr/share/locale 42252 /usr/share/doc 25600 /usr/share/vim 20292 /usr/share/perl5 18832 /usr/share/backgrounds 16232 /usr/share/man 9704 /usr/share/i18n 9196 /usr/share/cracklib 7236 /usr/share/hwdata 5544 /usr/share/mime # In positive numerical order (from small to large) [root@server ~]# du -s /usr/share/* | sort -n | head 0 /usr/share/gcc-4.8.5 0 /usr/share/magic 0 /usr/share/redhat-release 4 /usr/share/desktop-directories 4 /usr/share/dict 4 /usr/share/empty 4 /usr/share/file 4 /usr/share/firewalld 4 /usr/share/games 4 /usr/share/gnome
Use ll to print the file below / usr/bin, take the first 10 results, sort them with sort, and specify the first few fields with k option
[root@server ~]# ll /usr/bin/ | sort -nrk 1 | head Total consumption 305060 ---x--x--x 1 root root 57456 1 May 27, 2021 sudoreplay ---x--s--x 1 root nobody 382216 8 September 2019 ssh-agent ---s--x--x 1 root root 147336 1 May 27, 2021 sudo -r-xr-sr-x. 1 root tty 15344 6 October 2014 wall -rwx--s--x 1 root slocate 40520 4 November 2018 locate -rwxr-xr-x 2 root root 768648 4 June 24, 2019 x86_64-redhat-linux-gcc -rwxr-xr-x 2 root root 768648 4 June 24, 2019 gcc -rwxr-xr-x 2 root root 53329 1 June 22, 2019 s2p -rwxr-xr-x 2 root root 53329 1 June 22, 2019 psed [root@server ~]# ll /usr/bin/ | sort -nrk 2 | head Total consumption 305060 -rwxr-xr-x 2 root root 768648 4 June 24, 2019 x86_64-redhat-linux-gcc -rwxr-xr-x 2 root root 768648 4 June 24, 2019 gcc -rwxr-xr-x 2 root root 53329 1 June 22, 2019 s2p -rwxr-xr-x 2 root root 53329 1 June 22, 2019 psed -rwxr-xr-x 2 root root 44351 1 June 22, 2019 perlthanks -rwxr-xr-x 2 root root 44351 1 June 22, 2019 perlbug -rwxr-xr-x 2 root root 36607 1 June 22, 2019 pstruct -rwxr-xr-x 2 root root 36607 1 June 22, 2019 c2ph -rwxr-xr-x 2 root root 185504 6 October 2021 zipinfo [root@server ~]# ll /usr/bin/ | sort -nrk 5 | head -rwxr-xr-x 1 root root 19014016 12 October 2020 mysqlpump -rwxr-xr-x 1 root root 12972808 12 October 2020 mysql_upgrade -rwxr-xr-x 1 root root 11385136 12 October 2020 mysqlbinlog -rwxr-xr-x 1 root root 10743384 12 October 2020 mysql -rwxr-xr-x 1 root root 10295968 12 October 2020 myisamchk -rwxr-xr-x 1 root root 10194232 12 October 2020 mysql_install_db -rwxr-xr-x 1 root root 9907376 12 October 2020 myisampack -rwxr-xr-x 1 root root 9774584 12 October 2020 myisam_ftdump -rwxr-xr-x 1 root root 9767752 12 October 2020 mysqldump -rwxr-xr-x 1 root root 9691296 12 October 2020 mysqlcheck [root@server ~]# ll /usr/bin/ | sort -nrk 8 | head ---x--x--x 1 root root 57456 1 May 27, 2021 sudoreplay ---s--x--x 1 root root 147336 1 May 27, 2021 sudo -rwxr-xr-x 2 root root 185504 6 October 2021 zipinfo -rwxr-xr-x 2 root root 185504 6 October 2021 unzip -rwxr-xr-x 1 root root 90768 6 October 2021 unzipsfx -rwxr-xr-x 1 root root 32128 6 October 2021 funzip -rwxr-xr-x 1 root root 247512 1 May 27, 2021 cvtsudoers -rwxr-sr-x 1 root screen 475240 3 September 2021 screen lrwxrwxrwx 1 root root 9 3 September 2021 python2 -> python2.7 lrwxrwxrwx 1 root root 8 3 September 2021 systemd-loginctl -> loginctl
Sort the contents of / etc/passwd file, set the separator to colon, and sort according to the third field:
[root@server ~]# sort -t ":" -k 3 /etc/passwd | head root:x:0:0:root:/root:/bin/bash test:x:1000:1000::/home/test:/bin/bash aaa:x:1001:1001::/home/aaa:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
Edit a text file to show the following functions:
[root@server dir]# cat hahaha Alice 98 02/20/2021 Alice 97 01/31/2021 Alice 92 03/01/2020 Alice 95 07/11/2020 Bob 99 12/23/2021 Bob 91 02/25/2021 Bob 98 01/30/2020 Chris 98 02/19/2021 Chris 98 02/05/2020 Chris 94 02/08/2021 Chris 92 02/10/2021 Chris 93 02/13/2021 # Specify to sort by the first field and then by the second field, and the specified type is number [root@server dir]# sort -k 1,1 -k 2n hahaha Alice 92 03/01/2020 Alice 95 07/11/2020 Alice 97 01/31/2021 Alice 98 02/20/2021 Bob 91 02/25/2021 Bob 98 01/30/2020 Bob 99 12/23/2021 Chris 92 02/10/2021 Chris 93 02/13/2021 Chris 94 02/08/2021 Chris 98 02/05/2020 Chris 98 02/19/2021 # Specifies to sort by the 7th, 1st and 4th characters of the 3rd field in order [root@server dir]# sort -k 3.7 -k 3.1 -k 3.4 hahaha Bob 98 01/30/2020 Chris 98 02/05/2020 Alice 92 03/01/2020 Alice 95 07/11/2020 Alice 97 01/31/2021 Chris 94 02/08/2021 Chris 92 02/10/2021 Chris 93 02/13/2021 Chris 98 02/19/2021 Alice 98 02/20/2021 Bob 91 02/25/2021 Bob 99 12/23/2021