1. Overview

Sorting lines of text is a common task in Linux. In this tutorial, we’ll learn multiple ways of sorting lines of files in place on the terminal using the sort command.

2. Introduction to the sort Command

The sort command can help us to rearrange lines from standard input (stdin) or from a text file. The sort command will write the sorted result to standard output (stdout). It’s available in all Linux distros since it’s part of the GNU coreutils package.

The syntax of using the sort command is straightforward:

sort [OPTION]... [FILE]...

The sort utility will sort lines alphabetically by default:

$ cat cities.txt
New York City
Paris
Beijing
Hamburg
Los Angeles
Amsterdam

$ sort cities.txt
Amsterdam
Beijing
Hamburg
Los Angeles
New York City
Paris

However, if we pass different options, the sort command can do more than that, such as sort lines by number, in reverse order, by field, and so on.

We’ll go through some examples to learn how to sort files in place in various ways using the sort command.

3. Sorting In-Place With the sort Command

When we use the sort file command, the file shows its contents in a sorted way.

It doesn’t get any kind of output but a sorted file.

Let’s see how we can sort a file in place in the terminal.

3.1. Sorting Using Redirection with sort

Let’s take a look at STDOUT, the standard output stream of the sort command. All output from this stream is typically sent to our terminal window:

$ sort cities.txt
Amsterdam
Beijing
Hamburg
Los Angeles
NewYork City
Paris

We can use the > operator to direct output from a sort command to a temporary file:

$ sort cities.txt > cities_sorted.txt

Then, we can issue an mv command to revert the name back to cities.txt:

$ mv cities_sorted.txt cities.txt

Also, we can combine those two lines in one line:

$ sort cities.txt > cities_sorted.txt && mv cities_sorted.txt cities.txt

That’s it.

A common mistake is to try to redirect the output to the same input file (as in sort file > file). This does not work, as the shell – not the sort program – is making the redirections, and the input file (as being the output also) will be erased just before giving the sort program the opportunity of reading it.

3.2. Sorting Using sort Options

Another approach is to pass the -o FILE option to the sort command to write the result to a file instead of stdout:

$ sort -o cities.txt cities.txt

This overwrites the input file cities.txt with the sorted contents of the same file. The -o flag, used to specify an output, is defined by POSIX, so it should be available on all versions of sort.

The output file specified can be the same as one of the input files. In terms of functionality, it’s like redirecting the output to a file.

We can also avoid repeating the filename with bash brace expansion:

$ sort -o cities{,}

3.3. Sorting Using Command Substitution

As explained before, we cannot directly redirect the output back to the input file. But, we can evaluate the sort command first and then redirect it back to the original file. In this way, we can implement in-place sort:

$ echo "$(sort cities.txt)" > cities.txt

4. Conclusion

The sort command is a useful and straightforward command-line utility. In this article, we’ve learned some ways of using the sort command to sort files in place by examples.

Sorting by keys using the -o option is the best way to achieve file in-place sorting. It allows us to sort field-based data more flexibly.

With this handy tool in our command-line arsenal, we can solve most sorting problems easily.

Comments are closed on this article!