Rearrange with linux sort command

Order the contents of a file or an output with Linux sort command

One of the best ways to order the contents of a file or an output is to use the Linux sort command instead of making a script to do so and save administration time.

The sort command is one of the most typical commands that is present in Linux systems, which could help to order a list in alphabetical order.

Therefore, if you are not familiar with this useful command, continue reading to learn more about through some use cases described in this post.

Ordering in alphabetical order

If you run the sort command by default without any option, it will print the alphabetical order of a file in list format:

$ sort text_file
sorting an example text file in alphabetical order

This command does not only admits files as input, but also can rearrange the output from another command by using the pipe operator:

$ ls -ltra | sort
sort the output of other commands

In the above picture you can check the difference between the ls -ltra command and the same one but with sort processing.

Moreover, it also sorts the numbers without specifying any additional option:

examples of sorting list of numbers and characters

Sorting and removing duplicate values

The sort command provides the option -u that will remove duplicated values from the sorted output if any. In other words, the outcome will be similar to run sort combined with uniq:

$ sort -u words_file
Example of sort command with the -u option to remove duplicated values

Note that “Mouse” was the only word removed with the -u option but not the “Milk” word because they are different due to case sensitiveness.

If you want to perform the -u option case insensitive then, you need to combine it with the f option:

$ sort -fu words_file
Combining -u with -f to sort and remove duplicates in case insensitive mode.

This time, as the picture shows, even the word “Milk” was considered to be removed from the output despite their case difference.

Sorting by month order

Another option that could be interesting to consider is the -M which will tell the sort command to order by month order:

$ sort -M month_text
Ordering a list of months with sort -M command

As you can see in the previous image, even the abbreviated names of the month was taken into consideration by sort -M.

What if you want to remove duplicated months? Then, as you’ve learned, you can do it by adding the -u option:

$ sort -Mu month_text
Combining -M and -u to sort in month order and eliminate repeated months.

Note that the month “Jun” was removed because “June” was present. Therefore, sort with the options -Mu will remove first the abbreviated repeated months first.

Ordering in reverse mode

To flip the default order of sort command, you need to use or add the -r option:

$ sort -r word_file
Reversing the alphabetical order of sort with -r option

In the same way, it can be used with the month sorting:

$ sort -Mr month_text
reversing the sort command month order -M with -r

Execute sort to rearrange in random order

Finally, the sort -R option rearranges any input or file content in a random order, which could be useful in some situations where you need to process a file, line by line, but in different or random arranges.

$ sort -R word_text
Random arranging of a file with sort -R command

Therefore, each time you run sort with -R option, it should provide different arrangements of the same output or file content.