How to sort a file in-place?

LinuxBashShellUnix

Linux Problem Overview


When we use sort file command, the file shows its contents in a sorted way. What if I don't want to get any output on stdout, but a sorted file?

Linux Solutions


Solution 1 - Linux

You can use file redirection to redirected the sorted output:

sort input-file > output_file

Or you can use the -o, --output=FILE option of sort to indicate the same input and output file:

sort -o file file

Without repeating the filename (with bash brace expansion)

sort -o file{,}

⚠️ Note: A common mistake is to try to redirect the output to the same input file (e.g. sort file > file). This does not work as the shell is making the redirections (not the sort(1) program) and the input file (as being the output also) will be erased just before giving the sort(1) program the opportunity of reading it.

Solution 2 - Linux

The sort command prints the result of the sorting operation to standard output by default. In order to achieve an "in-place" sort, you can do this:

sort -o file file

This overwrites the input file with the sorted output. The -o switch, used to specify an output, is defined by POSIX, so should be available on all version of sort:

> -o Specify the name of an output file to be used instead of the standard output. This file can be the same as one of the input files.

If you are unfortunate enough to have a version of sort without the -o switch (Luis assures me that they exist), you can achieve an "in-place" edit in the standard way:

sort file > tmp && mv tmp file

Solution 3 - Linux

sort file | sponge file

This is in the following Fedora package:

moreutils : Additional unix utilities
Repo        : fedora
Matched from:
Filename    : /usr/bin/sponge

Solution 4 - Linux

Here's an approach which (ab)uses vim:

vim -c :sort -c :wq -E -s "${filename}"

The -c :sort -c :wq portion invokes commands to vim after the file opens. -E and -s are necessary so that vim executes in a "headless" mode which doesn't draw to the terminal.

This has almost no benefits over the sort -o "${filename}" "${filename}" approach except that it only takes the filename argument once.


This was useful for me to implement a formatter directive in a nanorc entry for .gitignore files. Here's what I used for that:

syntax "gitignore" "\.gitignore$"

formatter vim -c :sort -c :wq -E -s

Solution 5 - Linux

To sort file in place, try:

echo "$(sort your_file)" > your_file

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

Similarly, you can also apply this trick to other command like paste to implement row-wise appending.

Solution 6 - Linux

Do you want to sort all files in a folder and subfolder overriding them?

Use this:

find . -type f -exec sort {} -o {} \;

Solution 7 - Linux

No answers about few files, so:

sort -u file1 file2 -o file1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAli SajidView Question on Stackoverflow
Solution 1 - LinuxSylvain BugatView Answer on Stackoverflow
Solution 2 - LinuxTom FenechView Answer on Stackoverflow
Solution 3 - LinuxSilverlightFoxView Answer on Stackoverflow
Solution 4 - LinuxAnthony SottileView Answer on Stackoverflow
Solution 5 - LinuxBo TianView Answer on Stackoverflow
Solution 6 - LinuxluandreaView Answer on Stackoverflow
Solution 7 - LinuxstorenthView Answer on Stackoverflow