2

If I try truncate -s 0 log.log (:>log.log has same behavior) the displayd space on disk do become free but the size (ls -l) of file is still the same (tho du shows less). As far as I understand, it happens because the pointer is still "old".

This behavior causes that I cannot use cat ... | grep ... command: CLI says that file is binary. So the only way is to use less or another commands.

So, how do I truncate file, that is opened in write mode in another proccess and have correct file size after truncate? I need data in log.log to be truncated to another file, or just delete whole data in file without deleting the file itself

1 Answer 1

2

You cannot. The "next block to write" pointer is kept in the memory of the process that has the file open for writing. You can't access it from outside.

Some programs have been written to close old logs and open new ones on receipt of a signal.

Simply deleting the file, as the program is about to write block 1024, will result in 1023 blocks of zeros, and data in block 1024.

2
  • Okay, I think I got it. What if: 1. I have 1024 blocks only in log.log directory availble 2. I make 1023 blocks zeros. (truncate -s 0 log.log) 3. Original process will continue to write data to this file (because there is a free space on this disk after truncate), will it write data instead of this zeros? Like, is there any chanse that this zeros will become random data, by original process or not
    – Sova
    May 10, 2021 at 16:48
  • @Sova: Search the web for information on how file systems work and how operating systems handle open files. If one process has the file open, it has it's own pointer in to the file where the current read/write position is, and if the program is not written to detect changes to that pointer or file (very few programs do), then there is nothing you can do other than ending the processes, updating the file, and then restarting the process so it's file pointers are current.
    – C. M.
    May 10, 2021 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .