grep 'xxx' < /tmp/myFile.txt > /tmp/output.txt
Many commands follow this pattern: cat, grep, sed, awk, uniq etc.
There are a few special commands which use the -o flag to specify where to put their output: cc, as, and ld in particular. Note that these commands:
- produce binary output, which makes no sense to be displayed in a terminal window (and if you try, usually corrupts your terminal session)
- put their output into a specially-named file (a.out) if the -o flag is not specified, not to stdout
Given all this, the sort command is really weird, as it has an optional -o flag which specifies where to put the output, and if the -o flag is not specified sort sends its output to standard output.
I can't see any reason why sort has this flag, when the > redirection operator works just fine, and is used for this purpose in almost every other Unix command I know.
Does anybody know why sort has a -o flag, what purpose that flag serves that the output redirection operator doesn't serve, or whether there are any other Unix commands which follow this pattern?
Update: My co-worker Joe says that the sort command's implementation of -o is very special, because it is legal to specify the same file as both the output file and the input file. Apparently, the sort command, when processing its output using the -o argument, will write all the output to a file with a temporary name (thus not overwriting the input during the sort), and then at the end of the sort, it will rename the temporary file to the name given in the -o argument, thus allowing you to successfully sort a file back into itself. It's still not clear to me why sort is the only command which decided to have this behavior.