I want to sort a file in unix shell. Can I redirect my result into my input file?
e.g. if my input file is foo then can I use
sort foo > foo or I should use:
sort -o foo foo What would be difference between above two?
Thanks,
Use
sort -o foo foo From the man page:
-o OUTPUT-FILE' Write output to OUTPUT-FILE instead of standard output. If OUTPUT-FILE is one of the input files,sort' copies it to a temporary file before sorting and writing the output to OUTPUT-FILE.
sort foo > foo means writing output to the standard output which is redirected to the output file. Before redirecting, > will truncate/overwrite the output file if one is exist. Since both the input and output files are same, you will lose the input file information.