I have a log file of size nearly 100 MB which is opened by some program and logging is continuously going on in that file. I want to delete the first 10MB from that file. Is there any way to do that without affecting the logging process into that file.

  • 2
    Did the application open the log with O_APPEND or not (>> log as opposed to > log). Is it OK to suspend it for a few milliseconds? For a few seconds? Has it got a mechanism by which it can be told to reopen its log file (like many daemons do upon receiving HUP). Answers to those questions would help find the best solution. – Stéphane Chazelas Oct 5 '12 at 20:38
  • Another useful piece of information is if the program is open-source. – QuasarDonkey Oct 5 '12 at 21:41

To skip the first 10MB, you can use dd like that :

dd if=ORIGINAL_FILE of=10MB_LESS_FILE bs=512 skip=14880

That will copy the original file to 10MB_LESS_FILE.


There isn't a way to delete data from the start of a file in situ. The truncate call can trim data from the end, but there is no equivalent for trimming space arbitrarily.

Therefore the best you can do is top copy the file skipping over the bits you don't want.

  • tail is pretty close... – terdon Oct 6 '12 at 10:57
  • @terdon No, tail lets you extract a suffix of the file. It doesn't change the original file, which is what this question is about. – Gilles 'SO- stop being evil' Oct 6 '12 at 22:12
  • @Gilles, indeed, i just meant that tail is close to the truncate call in that it allows you to extract the end of the file, thereby essentially deleting the beginning. Of course it does not affect the original file. – terdon Oct 6 '12 at 22:15

You need to make sure that the inode of the file open by one or more processes doesn't change in order to continue logging to it. To do this, do:

cp mylog mylog.new
< modify, trim or mangle `mylog.new` >
cp mylog.new mylog
  • This doesn't answer the question on how to delete starting 10MB of file. – Krzysztof Adamski Oct 5 '12 at 19:31
  • When you delete file it won't be deleted from disk while it's used by some process and I'm not sure logging proccess will switch to the new file after deleting in this way. – rush Oct 5 '12 at 19:57
  • @rush That's correct, the space allocated to a file won't be returned to the free pool until the last process with the file open terminates. However, a simple experiment using my method will show that logging will continue, "...without affecting the logging process into that file". Contrast that with a direct edit of the file using vi, sed, etc. where the inode of the resulting file differs from the one in use by the process. – JRFerguson Oct 6 '12 at 15:09
  • @KrzysztofAdamski Correct. IMO the critical part of the OP question was how to trim "...without affecting the logging process into that file". – JRFerguson Oct 6 '12 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.