I want to watch a file that gets overwritten every 5 minutes with less
. How can I make less
follow the new file descriptor instead of keeping the old one displayed? watch "cat file"
won't do it because the file is too long to fit into one terminal window.
Bash – less with “update file” like functionality
bashlesswatch
Best Answer
You can get this effect by issuing the
F
command (Shift+F
) while viewing the file inless
. To stop following and switch back to paging, pressCtrl+C
Since your file only changes every 5 minutes, you could also use
tail -f
and specify a longer sleep time with-s
(defaults to 1 second). For instance,checks
myfile
for output every 60 seconds.EDIT: Due to misleading question, the above answer was unsatisfactory. Second attempt follows:
To re-open the same file in
less
every 5 minutes, try this:This will spawn a subshell which backgrounds another shell process instructed to kill its parent process after 5 minutes. Then it opens the file with
less
. When the backgrounded shell command kills the parent subshell, it kills all its children, including the "less" command. Then the loop starts the process over again.The only easy way I know of to kill this is to kill the terminal your original shell is in. If that's unacceptable, you can use the "killfile" trick:
To stop doing
stuff
,rm
the killfile in another shell.