Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting a file without using temporary files

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,

like image 822
shampa Avatar asked Jan 19 '23 13:01

shampa


1 Answers

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.

like image 130
Prince John Wesley Avatar answered Jan 27 '23 20:01

Prince John Wesley