Use sort command to sort pipeline data

Share this post to your friends !

This entry is part 4 of 10 in the series Linux commands: Part 6 - Redirection

WriteBash - This article will talk about sort command, how to use sort command to sort pipeline data?

In the pipeline article, you already know the power when combining commands into a pipeline. It allows you to transfer data in/out between commands.

Sort command syntax

The sort command used in pipeline, so we have the following command syntax:

$ command-1 | ... | sort | ... | command-n

You can understand that the sort command receives input data from the command right in front of it. Then it sorts this data. And finally output that data to the screen or to another command in the pipeline.

Example using sort command to sort pipeline data

In essence, the pipeline is a way to filter data. And the sort command is like its name, it will sort the pipeline data into a sorted list.

Let’s take a look at the following example, we use the ls command to list all the files in the /bin directory and /usr/bin directory.

The first is the result of the ls command without using sort.

use-sort-command-to-sort-pipeline-data-01 Use sort command to sort pipeline data
List content of some directories without using sort command.

And when we use sort command.

use-sort-command-to-sort-pipeline-data-02 Use sort command to sort pipeline data
Use sort command to sort pipeline data.

You can see, this command has combined the output data from two folders on the screen into one and made it sort. Then export that unique data to the screen.

In addition, the sort command also has its options. The option that we use a lot is -r, ie reverse sort.

$ command-1 | ... | sort -r | ... | command-n

You can type the command below to see all its options.

$ sort --help


This command is quite easy to use, its benefits in pipeline are huge. Jobs that require you to find a common denominator of data, then sorting data is essential.

Continue reading the series«« Previous part: Example of using cat command in LinuxNext part: Use uniq command to delete duplicate lines »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.

Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.