When we use the 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 in the input file instead?

- 7,704
- 3
- 29
- 30

- 3,964
- 5
- 18
- 33
-
Please update your question to clearly tell explain what you want to do with the result of sorting the file. Do you want to sort it in place, replacing the unsorted contents of `file` with the sorted contents of the same file? The way your question is currently stated, doing nothing at all is a correct answer (you don't want any output, so don't do anything). – Keith Thompson Mar 24 '15 at 22:59
-
@KeithThompson thanks for your concern actually, I was trying to sort a file but want to avoid the output on the screen. – Ali Sajid Mar 24 '15 at 23:01
-
So where do you want the output to go? Into a new file? Into the original file? To the printer down the hall? – Keith Thompson Mar 24 '15 at 23:04
-
1Into the orignal file – Ali Sajid Mar 24 '15 at 23:06
-
run `sort infile >outfile` and you'll get the sorted output into file "outfile". Then you can erase the original "infile" and everything will be ok. – Luis Colorado Mar 25 '15 at 12:03
-
1@AliSajid: Then please update your question to say that, so your question is useful for other readers with the same problem. – Keith Thompson Mar 25 '15 at 15:12
7 Answers
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{,}
⚠️ Important 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.

- 7,704
- 3
- 29
- 30
-
9Just to mention the importance of order. From `GNU sort` info page: `-o OUTPUT-FILE' `--output=OUTPUT-FILE' Write output to OUTPUT-FILE instead of standard output. 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. – Yassine ElBadaoui Oct 06 '17 at 07:48
-
1The first example code line should be deleted. It is _not at all_ what the title of the question refers to: `in-place` sorting. This redirection could go last with the note. But definitely _not_ first. – Cornelius Roemer Jun 01 '22 at 12:35
-
1I made the same mistake, too. Actually I made a backup of the file before that, so my data is intact. But I was wondering why I ended up with an empty file. Thanks for the explaination. – Livy Jun 25 '22 at 16:10
-
Thanks for the nice brace expansion trick I learned today from you. My mistake was that I used the long option `--output`. I forgot an additional hyphen in my hurry also put the output option last, which is what you would do when you are used to redirection. When you do everything properly it just works of course. :-) – Benjamin Jul 15 '23 at 11:56
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

- 18,333
- 31
- 67
- 74

- 72,334
- 12
- 107
- 141
sort file | sponge file
This is in the following Fedora package:
moreutils : Additional unix utilities
Repo : fedora
Matched from:
Filename : /usr/bin/sponge

- 32,436
- 11
- 76
- 145
-
3This also works on Ubuntu after installing with `sudo apt install moreutils` – Serge Stroobandt Oct 07 '19 at 17:39
Do you want to sort all files in a folder and subfolder overriding them?
Use this:
find . -type f -exec sort {} -o {} \;

- 301
- 2
- 8
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

- 61,815
- 15
- 148
- 207
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.

- 277
- 2
- 6
-
7This is bad because it will not work for large files, strips ending newlines, `echo` can mess with backslash escapes in POSIX, and `-o` does work for the same file as input as explained at: https://stackoverflow.com/a/29244387/895245 – Ciro Santilli OurBigBook.com Apr 08 '19 at 10:34