Sometimes I am running a less +F
on a log file, which I then truncate. I would like less
to realize that the file has been truncated and start following the file as it is now. Currently, I am forced to do the following:
- (terminal 1) >
less +F my-file.txt
- (terminal 2) >
printf "" > my-file.txt
- (terminal 1) > quit less
- (terminal 1) > restart less:
less +F my-file.txt
And I have to do steps 3-4 every time that I truncate the file (which is dozens o times a day). This is really a pain.
I would like to do:
- (terminal 1) >
less +F my-file.txt
(and whatever extra option is needed) - (terminal 2) >
printf "" > my-file.txt
And that's it. less
would be automatically aware that the file has been truncated, and start following the new file from the beginning.
Is this possible? What flag should I use for less to achieve this?
Not precisely the answer you were looking for, but you could try using 'tail -F' (As opposed to 'tail -f') which will give you exactly the behavior you're looking for.
Why don't you combine them like so?
tail -F filename | tee | less
This works well for me.
You want to specify the
--follow-name
argument toless
. From the man page:So your steps would now be:
less --follow-name +F my-file.txt
printf "" > my-file.txt
P.S. Instead of using printf, lots of shells will truncate with just the redirection operator:
> my-file.txt