Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Am I blind or is there no option like --in-place for sort?

In order to save results to the input file, sed uses -i (--in-place).

Redirecting the output of sort to the input file

sort < f > f

results in making it empty. If there is no --in-place option - maybe there is some trick how to do this in handy way?

(The only thing that cames to my mind:

sort < f > /tmp/f$$ ; cat /tmp/f$$ f ; rm /tmp/f$$

Moving is not right choice, cause file permissions might be changed. That's why I overwrite it with contents of tmp which I then remove.)

share|improve this question
There is also insitu, allowing any commands to be used in-place. – sr_ Jan 22 '12 at 10:19
@sr_, that's an interesting command, but it doesn't work with any command, only ones that write no faster than they read (otherwise it'll clobber the input file before the command reads it). There's no guarantee that it'll work with sort. – cjm Jan 22 '12 at 11:09
@cjm, I'm really not sure, but isn't this supposed to handle that case? – sr_ Jan 22 '12 at 13:39
@sr_, I think you're right. I read the description instead of looking at the source. Although for really big files, it might run out of memory for the buffer and crash (it doesn't look like it checks for a NULL return from malloc). – cjm Jan 22 '12 at 14:02
@cjm: Oh yes, indeed. – sr_ Jan 22 '12 at 14:19
up vote 47 down vote accepted

sort has the -o, --output option that takes a filename as argument. If it is the same as the input file, it writes the result to a temporary file, then overwrites the original input file (exactly the same thing as what sed -i does).

From GNU sort info page:

      Write output to OUTPUT-FILE instead of standard output.  Normally,
      `sort' reads all input before opening OUTPUT-FILE, so you can
      safely sort a file in place by using commands like `sort -o F F'
      and `cat F | sort -o F'.  However, `sort' with `--merge' (`-m')
      can open the output file before reading all input, so a command
      like `cat F | sort -m -o F - G' is not safe as `sort' might start
      writing `F' before `cat' is done reading it.

      On newer systems, `-o' cannot appear after an input file if
      `POSIXLY_CORRECT' is set, e.g., `sort F -o F'.  Portable scripts
      should specify `-o OUTPUT-FILE' before any input files.

and from The Open Group Base Specifications Issue 7:

-o  output
    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.
share|improve this answer
Exactly ! It works ! I can't see any clue about it in man sort - is it undocumented feature ? Is it standard and portable ? – Grzegorz Wierzowiecki Jan 22 '12 at 10:13
@GrzegorzWierzowiecki: see update. – enzotib Jan 22 '12 at 10:24
Nice answer :). – Grzegorz Wierzowiecki Jan 22 '12 at 11:12
In summary: sort -o <filename> <filename> will safely sort a file in place. – phyatt Mar 1 at 14:52

It's dangerous to overwrite the input file with the output file, because if the program or the system crashes while the file is being written, you've lost both.

A few programs (mostly GNU versions) have an in-place option (e.g. -i on perl and GNU sed; -o on GNU sort). They work by putting the data in a temporary file and then moving it into place. For programs that have no such option, Colin Watson's sponge utility (included in Joey Hess's moreutils) does the job safely for any program (examples: Can I make cut change a file in place?; How can I make iconv replace the input file with the converted output?).

Only in those rare cases where you can't recreate the original file with the same permissions do I recommend overwriting the file in place. In this case, you'd better save the original input somewhere. And then you can simply process the copy of the input and send it into the original file.

cp -p f ~/f.backup
sort <~/f.backup >|f
rm ~/f.backup # optional
share|improve this answer
sort -o is not GNU specific, and is especially designed to orverride the file in place. sort cannot start writing its output before it has read its inputs fully (uses memory or temporary files to store data), so it comes quite naturally that it should be able to override its input. – Stéphane Chazelas Nov 8 '15 at 15:47
And actually, it's one case where GNU sort is not POSIX as sort -mo file1 file1 file2 is not guaranteed to work while traditional sorts know how to work around that (alread in Unix V7 in the 70s). – Stéphane Chazelas Nov 8 '15 at 15:56

Use -o or try the vim-way:

$ ex -s +'%!sort' -cxa file.txt
share|improve this answer

You can use the sponge function, that first soaks the stdin and then writes it to a file, like:

sort < f | sponge f

The downside of sponge is that it will store the output temporary in memory, which can be problematic for large files. Otherwise you have to write it to a file first and then overwrite the original file.

As however is pointed out by other answers, in place modifications are in general not a good idea, since in the middle of a process (for instance the sponge one), the machine might crash and then you can lose both the original and new file. You better first write it to a different file and then use an atomic mv (move) instruction.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.