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
Table of Contents
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
.
And when we use sort
command.
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
Conclusion
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).