3 ms·
> Why not use > file? I'm assuming sponge buffers everything in memory so you don't get concurrent modification bugs.
by gravypod 6y ago
> Why not use > file?
I'm assuming sponge buffers everything in memory so you don't get concurrent modification bugs.
- masklinn 6y agoYes, if you try to sed a file back to itself for instance you'll get a truncated file (or maybe a no-op?). In fact, that's exactly the example sponge's manpage uses.
- lloeki 6y agoThat redirection will make the shell truncate the file long before sed has a chance to open it.
- moonchild 6y agoIt's a truncated file: $ cat test moo $ cat < test > test $ cat test $
- Izkata 6y agoI was wondering about tee for that: $ cat test moo $ cat < test | tee test > /dev/null $ cat test moo ...seems to work. Am I just getting lucky with a race condition?
- notimetorelax 6y agoPossible, since you don’t pipe into the file but instead ask tee to write into it.
- Erwin 6y agoYes, check this, with f.txt containing 99999 numbers: $ seq 1 99999 > f.txt $ cat < f.txt | tee f.txt > /dev/null $ wc -l f.txt 23696 f.tx
- moonchild 6y ago> Am I just getting lucky with a race condition? Probably this is buffering, either from dd or from the kernel. $ dd if=/dev/urandom bs=1M count=1 of=test.bin 2>/dev/null $ cp test.bin test2.bin $ cat < test2.bin | tee test2.bin >/dev/null $ diff test.bin test2.bin Binary files test.bin and test2.bin differ $ wc -c test{2,}.bin 131072 test2.bin 1048576 test.bin Notice the file got truncated at exactly 128k; a nice round size for a write cache.
- kasabali 6y agoI think using tac | tac is a safe bet since must be reading all file before by definition.