Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sort to the same output file: sort -o foo.txt foo.txt

You can't do sort foo > foo because you'll be left with a blank file.



sponge(1) from "moreutils"

Although sort(1) handles this for you as you mention, the general problem in the shell of wanting to overwrite your input file with the output file, but not being able to redirect to it for the reason you mention, is solved with sponge. Rather than:

$ grep "foo" bar.txt >baz.txt; mv baz.txt bar.txt

instead:

$ grep "foo" bar.txt | sponge bar.txt




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: