Use sort command to sort pipeline data

by Daniel Pham
Published: Updated:
This entry is part 4 of 10 in the series Linux commands: Part 6 - Redirection

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.

Recommended Reading: Overview of I/O redirection in Linux

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

list content of some directories without using sort command
List content of some directories without using sort command.

And when we use sort command.

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.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series Navigation«« Previous part: Example of using cat command in LinuxNext part: Use uniq command to delete duplicate lines »»

You may also like

Notify of
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.



Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 – All rights reserved.

Please write sources “” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Would love your thoughts, please comment.x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.