Take the 2-minute tour ×
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.

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

2 Answers 2

up vote 23 down vote accepted

sort has the -o, --output option that take a filename as argument. If it is the same as the input files, it write the result to a temporary file, then overwrite the original input file (somewhat as sed do).

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

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

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.