There is no simple way to remove lines from the beginning of file !
Even by using sed -i
, you create a new file as shown with the following commands (>
is my prompt):
> echo "Helo World" > toto
> ls -i toto
147543 toto
> sed -i -e 's/Helo/Hello/' toto
> ls -i toto
147292 toto
Notice that the inode number is not the same. This means that you create a new file with the same name, not that you modify the file in place.
This is important if your log file is open by a program while you perform this operation. If it is, you will create a new file while the program holding the file will keep writing to the old file. To show this, let's try the following:
for f in $(seq 1 100); do date; echo $f; sleep 1; done > file1&
ln file1 file2
sleep 5
sed -i -e '1,10d' file1
ls -l file1 file2
sleep 5
ls -l file1 file2
The 2nd ls
will show the same size for file1
and a growing size for file2
. If I had not done a ln
before executing sed
, the original file would have keep growing without being accessible via the file system hierarchy. This would result in use space on disk as shown by df
but not shown by du
. More information can be found here and here.
Log rotation is your friend here, but it cannot be done without help from the logging program. There should be a way to tell the program to close and reopen the file, so the new file would be used, but the log written after the beginning of the sed
and the end of reopening the file could be lost. If you do not want to loose logs, you can copy the file first, ask for the program to reopen the file, and then modify the copied file. This is what logrotate
allows you to do with minimal scripting.
You can read more on this subject here (apache 1.3), here (apache 2.4) and here (bind 9).