5 Interesting Linux ‘sort’ Command Examples


If you're a Linux user, you're probably familiar with command-line interface (CLI). CLI offers powerful tools to perform complex tasks quickly and efficiently. One of most useful CLI tools is 'sort' command. 'sort' command allows you to sort lines of text in alphabetical or numerical order, and it offers a variety of options that can make your life easier. In this article, we'll explore five interesting Linux 'sort' command examples.

Sort by Numeric Values

The 'sort' command can be used to sort data in numerical order. This is useful when dealing with datasets that include numerical values. To sort by numeric values, use '-n' option. Here's an example −

$ cat numbers.txt
10
2
30
4
20

$ sort -n numbers.txt
2
4
10
20
30

In this example, 'sort' command sorts 'numbers.txt' file in numerical order using '-n' option.

Sort in Reverse Order

Sometimes, you may want to sort data in reverse order. For instance, you may want to sort a list of files by date and time, starting with newest files first. To sort in reverse order, use '-r' option. Here's an example −

$ ls -l
total 0
-rw-r--r-- 1 user user 0 Mar 23 14:15 file1.txt
-rw-r--r-- 1 user user 0 Mar 23 14:14 file2.txt
-rw-r--r-- 1 user user 0 Mar 23 14:13 file3.txt

$ ls -l | sort -r
total 0
-rw-r--r-- 1 user user 0 Mar 23 14:15 file1.txt
-rw-r--r-- 1 user user 0 Mar 23 14:14 file2.txt
-rw-r--r-- 1 user user 0 Mar 23 14:13 file3.txt

In this example, 'ls -l' command lists files in current directory, and 'sort -r' command sorts list in reverse order.

Sort by Column

The 'sort' command can also sort data by specific columns. This is useful when dealing with datasets that have multiple columns of data. To sort by a specific column, use '-k' option. Here's an example −

$ cat names.txt
John Smith,25
Mary Johnson,30
Bob Jones,20
Tom Davis,35

$ sort -t ',' -k 2 names.txt
Bob Jones,20
John Smith,25
Mary Johnson,30
Tom Davis,35

In this example, 'names.txt' file includes two columns of data separated by a comma. 'sort' command uses '-t' option to specify field separator (the comma), and '-k 2' option to sort by second column (the age column).

Sort by Unique Values

The 'sort' command can also remove duplicate lines from a dataset. To do this, use '-u' option. Here's an example −

$ cat fruits.txt
apple
orange
banana
orange
apple

$ sort -u fruits.txt
apple
banana
orange

In this example, 'sort' command removes duplicate lines from 'fruits.txt' file using '-u' option.

Sort and Merge Files

The 'sort' command can also merge multiple sorted files into a single sorted file. This is useful when dealing with large datasets that have been split into multiple files. To merge files, use '-m' option. Here's an example −

$ cat file1.txt
1
3
5

$ cat file2.txt
2
4
6

$ sort -m file1.txt file2.txt
1
2
3
4
5
6

In this example, 'sort' command merges 'file1.txt' and 'file2.txt' files into a single sorted file using '-m' option.

Sort by Ignoring Leading Characters

Sometimes, a dataset may have leading characters that you don't want to consider while sorting. For example, you may have a list of filenames that begin with a date stamp, and you want to sort them by filename only. To ignore leading characters, use '-b' option. Here's an example −

$ ls
2023-03-22_file1.txt
2023-03-23_file3.txt
2023-03-21_file2.txt

$ ls | sort -b -t '_' -k 2
2023-03-22_file1.txt
2023-03-21_file2.txt
2023-03-23_file3.txt

In this example, 'ls' command lists files in current directory. 'sort' command uses '-b' option to ignore leading spaces, and '-t' option to specify field separator (the underscore). '-k 2' option sorts files by second field (the filename) while ignoring leading date stamp.

Sort by Human-readable Sizes

If you're working with files or directories, you may want to sort them by size. However, sorting by size in bytes can be difficult to read and compare. To sort by human-readable sizes (e.g. KB, MB, GB), use '-h' option. Here's an example −

$ du -h
10M     file1.txt
100K    file2.txt
1G      file3.txt

$ du -h | sort -h
100K    file2.txt
10M     file1.txt
1G      file3.txt

In this example, 'du' command displays disk usage of files in current directory in human-readable format using '-h' option. 'sort' command sorts files by size in human-readable format using '-h' option.

Sort by Date and Time

If you're working with files, you may want to sort them by date and time. To sort by date and time, use '-t' option to specify field separator, and '-k' option to sort by date and time column. Here's an example −

$ ls -l
-rw-r--r-- 1 user user 0 Mar 23 14:15 file1.txt
-rw-r--r-- 1 user user 0 Mar 22 13:45 file2.txt
-rw-r--r-- 1 user user 0 Mar 21 12:30 file3.txt

$ ls -l | sort -t ' ' -k 6,7
-rw-r--r-- 1 user user 0 Mar 21 12:30 file3.txt
-rw-r--r-- 1 user user 0 Mar 22 13:45 file2.txt
-rw-r--r-- 1 user user 0 Mar 23 14:15 file1.txt

In this example, 'ls -l' command lists files in current directory with their date and time of modification. 'sort' command uses '-t' option to specify field separator (a space), and '-k 6,7' option to sort by date and time columns.

Sort by Random Order

Finally, you can also sort data in a random order. This can be useful when testing or generating random data. To sort in random order, use '-R' option. Here's an example −

$ cat names.txt
John
Mary
Bob
Tom

$ sort -R

In this example, 'sort' command sorts names in 'names.txt' file in a random order using '-R' option. This can be useful for generating random test data or shuffling lists.

Conclusion

The 'sort' command is a powerful tool for sorting and manipulating data on Linux command line. In this article, we've explored five interesting 'sort' command examples, including sorting by numeric values, sorting in reverse order, sorting by column, sorting by unique values, and merging sorted files. These examples should give you a good starting point for using 'sort' command in your own Linux command-line workflows. Happy sorting!

Updated on: 11-Apr-2023

127 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements