Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't "sort file1 > file1" work?

When I am trying to sort a file and save the sorted output in itself, like this

sort file1 > file1;

the contents of the file1 is getting erased altogether, whereas when i am trying to do the same with 'tee' command like this

sort file1 | tee file1;

it works fine [ed: "works fine" only for small files with lucky timing, will cause lost data on large ones or with unhelpful process scheduling], i.e it is overwriting the sorted output of file1 in itself and also showing it on standard output.

Can someone explain why the first case is not working?

like image 705
dig_123 Avatar asked Oct 28 '11 22:10

dig_123


People also ask

Which command works on sorted files?

SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically. SORT command sorts the contents of a text file, line by line.

How does the sort command work?

The sort command is used in Linux to print the output of a file in given order. This command processes on your data (the content of the file or output of any command) and reorders it in the specified way, which helps us to read the data efficiently.


1 Answers

As other people explained, the problem is that the I/O redirection is done before the sort command is executed, so the file is truncated before sort gets a chance to read it. If you think for a bit, the reason why is obvious - the shell handles the I/O redirection, and must do that before running the command.

The sort command has 'always' (since at least Version 7 UNIX) supported a -o option to make it safe to output to one of the input files:

sort -o file1 file1 file2 file3

The trick with tee depends on timing and luck (and probably a small data file). If you had a megabyte or larger file, I expect it would be clobbered, at least in part, by the tee command. That is, if the file is large enough, the tee command would open the file for output and truncate it before sort finished reading it.

like image 196
Jonathan Leffler Avatar answered Oct 04 '22 02:10

Jonathan Leffler