Say I have some process which pipes to a file:yes > fooI know want to clear/empty the file foo, but still want the process to write to the file, i.e. start filling the file again after clearing.Is this possible?
How can I clear/empty a file being piped to?
bash;pipe;io redirection
Use yes >> foo instead of yes > foo and separately :>foo to clear the fileyes >> foo cause read to open foo with O_APPEND. This causes the file offset to be positioned at the end of the file before each write.So yes writes to the file foo, with each write causing the file pointer to be positioned at the end of foo. Meanwhile : > foo truncates the file to 0 bytes. Now the next yes write, due to the re-positioning of file pointer at the end of the zero-byte foo induced by O_APPEND, starts over at the beginning of the zero-byte foo 