How to truncate a file while it is being used?
Ideally, you should ask the vendor of the application to open the log file with the O_APPEND flag. This means that after you truncate the file, the next write will implicitly seek to the end of the file (meaning back to offset zero) and then write the new information.
What happens when a file is truncated to zero length?
This happens because the file is truncated to zero length, but the file descriptor in the application still points immediately after its last write. When it writes again, the file system treats the start of the file as all zero bytes – without actually writing the zeroes to disk.
Is there a way to truncate a log file?
If you want to truncate/zero a log file to which you don’t have write access, you can do it is a powerful tool which gives configurable options for rotating logs. copytruncate enables you to copy existing files and then truncate it. The copy can be moved to another storage such as hadoop, s3 for backup if desired
What does the truncate command do in Linux?
truncate Command truncate is a command-line utility that allows you to shrink or extend the size of a file to a given size. The general syntax for truncating files to zero size with the truncate command, is as follows: truncate -s 0 filename
Is there a way to truncate a file in Bash?
On most modern shells such as Bash or Zsh you can omit the command before the redirection symbol and use: To be able to truncate a file, you need to have write permissions on the file. Usually, you would use sudo for this, but the elevated root privileges do not apply to the redirection.
Why does tail not print the last line?
The problem is that tail program buffers its input file until it get a eof condition, then it prints out the last lines (10 by default). Most likely you are interrupting it with ctrl-c combination that terminates it, thus the tail has no chance to print out the collected lines.