{"id":4714,"date":"2021-02-23T11:34:40","date_gmt":"2021-02-23T11:34:40","guid":{"rendered":"https:\/\/linuxways.net\/?p=4714"},"modified":"2021-02-23T16:57:08","modified_gmt":"2021-02-23T16:57:08","slug":"sort-command-in-linux-with-examples","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/sort-command-in-linux-with-examples\/","title":{"rendered":"Sort command in Linux with examples"},"content":{"rendered":"<p>The sort command is one of the GNU core utilities that is used to sort content in a particular order. It can sort the content in a number of ways that include alphabetically, numerically, by month, in reverse order etc. Here are some key points you need to keep in mind regarding the sort command:<\/p>\n<ul>\n<li>Digits appear first before letters<\/li>\n<li>For each letter, lowercase are sorted first.<\/li>\n<li>By default, spaces are used as a field separator. Other field separators can be used as well.<\/li>\n<\/ul>\n<p>In this post, we will be showing you the basic usage and some common options of the sort command in Linux. We will be testing all the commands on <a href=\"https:\/\/linuxways.net\/de\/category\/ubuntu\/\"><strong>Ubuntu 20.04 LTS<\/strong><\/a>. However, you can apply the same commands on other Linux distributions.<\/p>\n<p>The syntax of the sort command is as follows:<\/p>\n<pre>$ sort [OPTION]... [FILE]...<\/pre>\n<h2>Basic Usage of the Sort Command<\/h2>\n<p>Using the sort command without any command-line options sorts the content of a specified file alphabetically.<\/p>\n<p>Below is our <strong>sample1.txt<\/strong> file that we will be using as an example.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"418\" height=\"171\" class=\"wp-image-4715\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-353.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-353.png 418w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-353-300x123.png 300w\" sizes=\"auto, (max-width: 418px) 100vw, 418px\" \/><\/p>\n<p>To sort the content of the <strong>sample1.txt<\/strong> file, the command would be:<\/p>\n<pre>$ sort sample1.txt<\/pre>\n<p>Here is the output of the above command which shows the alphabetically sorted content.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"427\" height=\"167\" class=\"wp-image-4716\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-alphabetically.png\" alt=\"sort alphabetically\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-alphabetically.png 427w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-alphabetically-300x117.png 300w\" sizes=\"auto, (max-width: 427px) 100vw, 427px\" \/><\/p>\n<p>If the file contains both alphabets and numbers, then the sort command will sort numbers before alphabets. Below is our sample file which contains both numbers and alphabets:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"415\" height=\"265\" class=\"wp-image-4717\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-354.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-354.png 415w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-354-300x192.png 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/p>\n<p>Here is the output of the above file after sorting where you can see the numbers are sorted before the alphabets.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"419\" height=\"265\" class=\"wp-image-4718\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-355.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-355.png 419w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-355-300x190.png 300w\" sizes=\"auto, (max-width: 419px) 100vw, 419px\" \/><\/p>\n<h2>Sort Command Options<\/h2>\n<p>The sort command provides a lot of options that expand its usefulness. Let\u2019s have a look at few of the options and how to use them.<\/p>\n<h3>Sort in Reverse Order<\/h3>\n<p>To sort the file content in reverse order, use the <strong>-r<\/strong> option. For example, below is the content of our sample file which we want to sort in reverse alphabetical order.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"425\" height=\"170\" class=\"wp-image-4719\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-356.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-356.png 425w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-356-300x120.png 300w\" sizes=\"auto, (max-width: 425px) 100vw, 425px\" \/><\/p>\n<p>To sort the content of this file in reverse order, use the <strong>-r<\/strong> option as follows:<\/p>\n<pre>$ sort -r sample1.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"459\" height=\"174\" class=\"wp-image-4720\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/reverse-sort.png\" alt=\"reverse sort\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/reverse-sort.png 459w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/reverse-sort-300x114.png 300w\" sizes=\"auto, (max-width: 459px) 100vw, 459px\" \/><\/p>\n<h3>Sort in Numeric Order<\/h3>\n<p>To sort the data numerically, use the <strong>-n<\/strong> option with the sort command. This option is useful if your file contains numbers and you want to sort them from lowest number to highest or from highest to lowest. Keep in mind that in default sorting, 113 would be considered smaller than 2.<\/p>\n<p>Below is an example of a default sorting of our sample file. Keep in mind that in default sorting, sorting is done on the basis of the first number. If the first number is the same, then it sorts on the basis of the second number. Like in the below example, 113 is smaller than 13 and 2.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"444\" height=\"196\" class=\"wp-image-4721\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-357.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-357.png 444w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-357-300x132.png 300w\" sizes=\"auto, (max-width: 444px) 100vw, 444px\" \/><\/p>\n<p>To sort the above content numerically (from lowest number to highest), use the <strong>-n<\/strong> option as follows:<\/p>\n<pre>$ sort -n sample2.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"451\" height=\"194\" class=\"wp-image-4722\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/numeric-sort.png\" alt=\"numeric sort\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/numeric-sort.png 451w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/numeric-sort-300x129.png 300w\" sizes=\"auto, (max-width: 451px) 100vw, 451px\" \/><\/p>\n<h3>Sort by Month<\/h3>\n<p>The default sort command sorts the file content alphabetically. If a file contains a list of month&#8217;s names, you can sort them month-wise using the <strong>-M<\/strong> command. For example, below is our sample file containing the month names.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"411\" height=\"215\" class=\"wp-image-4723\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-358.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-358.png 411w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-358-300x157.png 300w\" sizes=\"auto, (max-width: 411px) 100vw, 411px\" \/><\/p>\n<p>To sort the above file content by months, use the <strong>-M<\/strong> option as follows:<\/p>\n<pre>$ sort -M sample3.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"467\" height=\"222\" class=\"wp-image-4724\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-month-wise.png\" alt=\"sort month wise\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-month-wise.png 467w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-month-wise-300x143.png 300w\" sizes=\"auto, (max-width: 467px) 100vw, 467px\" \/><\/p>\n<h3><strong><strong>Sort on the Basis of Column Number<\/strong><\/strong><\/h3>\n<p>By default, the sort command starts sorting the content on the basis of the first character of the first column.<\/p>\n<p>For example, below is our sample file which contains two columns; fruit names and their respective prices. If we use default sorting, it would be done based on the first column (containing fruit names).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"465\" height=\"170\" class=\"wp-image-4725\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-359.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-359.png 465w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-359-300x110.png 300w\" sizes=\"auto, (max-width: 465px) 100vw, 465px\" \/><\/p>\n<p>To sort the content of the above file on the basis of the second column(prices), we will have to use the -k command as follows:<\/p>\n<pre>$ sort -k2 -n sample1.txt<\/pre>\n<p>Where -k2 tells the sort command to sort on the second column and -n is used for numerical sort.<\/p>\n<p>The above command will sort the content based on their prices (from lower to higher).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"499\" height=\"173\" class=\"wp-image-4726\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-colmun-wise.png\" alt=\"sort colmun wise\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-colmun-wise.png 499w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-colmun-wise-300x104.png 300w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/p>\n<h3>Use Different Field Separator<\/h3>\n<p>By default, the sort command uses space as a field separator. To use a different field separator other than space, use the <strong>-t<\/strong> option. Below is our sample file that we want to sort on the basis of the second column (separated by <strong>commas<\/strong>).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"174\" class=\"wp-image-4727\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-360.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-360.png 470w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-360-300x111.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/p>\n<p>The command to sort the above file on the basis of the second column would be:<\/p>\n<pre>$ sort -k2 -n -t \u201c,\u201d sample1.txt<\/pre>\n<p>Where -k2 tells the sort command to sort on the second column, -n is used for numerical sort and -t is used to specify the field separator.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"586\" height=\"170\" class=\"wp-image-4728\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-using-different-filed-separator.png\" alt=\"sort using different filed separator\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-using-different-filed-separator.png 586w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-using-different-filed-separator-300x87.png 300w\" sizes=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/p>\n<h3><strong><strong>Sort and Remove Duplicates<\/strong><\/strong><\/h3>\n<p>The <strong>-u<\/strong> option with sort command also removes the duplicates along with sorting the content. Below is our sample file which contains the duplicate values.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"420\" height=\"198\" class=\"wp-image-4729\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-361.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-361.png 420w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-361-300x141.png 300w\" sizes=\"auto, (max-width: 420px) 100vw, 420px\" \/><\/p>\n<p>To sort this file alphabetically and remove the duplicate values as well, the command would be:<\/p>\n<pre>$ sort -u sample1.txt<\/pre>\n<p>This is the output of the sorted file with removed duplicates.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"461\" height=\"173\" class=\"wp-image-4730\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-and-remove-duplicates.png\" alt=\"sort and remove duplicates\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-and-remove-duplicates.png 461w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-and-remove-duplicates-300x113.png 300w\" sizes=\"auto, (max-width: 461px) 100vw, 461px\" \/><\/p>\n<h3>Check for Sorted Input<\/h3>\n<p>Using the <strong>-c<\/strong> option with the sort command, you can check if a file is earlier sorted or not. If a file is earlier sorted, you will see no output. However, if the file is not sorted, it will show the lines which are out of order.<\/p>\n<p>Here is our sample file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"417\" height=\"100\" class=\"wp-image-4731\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-362.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-362.png 417w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-362-300x72.png 300w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/p>\n<p>To check whether this file is already sorted or not, use the <strong>-c<\/strong> option as follows:<\/p>\n<pre>$ sort -c sample.txt<\/pre>\n<p>As our sample file was already sorted, therefore no output is displayed.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"517\" height=\"50\" class=\"wp-image-4732\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/check-sorting.png\" alt=\"check sorting\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/check-sorting.png 517w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/check-sorting-300x29.png 300w\" sizes=\"auto, (max-width: 517px) 100vw, 517px\" \/><\/p>\n<h3>Save Sorted Output in a File<\/h3>\n<p>Sort command only displays the sorted output on Terminal; it does not save it. To save the sorted output in a separate file, use the redirect operator. For example, to save the sorted output of <strong>file.txt<\/strong> in a new file named <strong>sortedfile.txt<\/strong>, use the below command:<\/p>\n<pre>$ sort file.txt &gt; sortedfile.txt<\/pre>\n<h3>Sort Command using Pipe Option<\/h3>\n<p>You can sort the output of other commands by piping them to the sort command. For example, we want to numerically sort a file\/directory listing in the Home directory by their sizes. We can do so by using the <em>ls<\/em> command and piping its output to sort the command as follows.<\/p>\n<pre><strong>$ ls -l \/home\/kbuzdar\/ | sort -nk5<\/strong><\/pre>\n<p>Here is the output of the above command where you can see files are sorted by their sizes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"803\" height=\"342\" class=\"wp-image-4733\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-command-using-pipe-option.png\" alt=\"Sort command using pipe option\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-command-using-pipe-option.png 803w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-command-using-pipe-option-300x128.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/sort-command-using-pipe-option-768x327.png 768w\" sizes=\"auto, (max-width: 803px) 100vw, 803px\" \/><\/p>\n<p>In this post, we explained the basic syntax and usage of the sort command in Linux. We also explained some of the common options to extend its functionality. To view help and learn about more sort options, visit the sort <a href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/sort.1.html\">man page<\/a> or type <em>sort &#8211;help<\/em> in Terminal.<\/p>","protected":false},"excerpt":{"rendered":"<p>The sort command is one of the GNU core utilities that is used to sort content in a particular order. It can sort the content in a number&hellip;<\/p>","protected":false},"author":4,"featured_media":4734,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[207,100],"class_list":["post-4714","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-sort-command-in-linux","tag-ubuntu-20-04"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=4714"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4714\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/4734"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=4714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=4714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=4714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}