11

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.

4
  • 2
    "Follow" implies following the end of the file. You may want to clarify that your file gets overwritten and you are interested in reopening it.
    – chutz
    Nov 6, 2012 at 14:48
  • I meant following the file descriptor, as it is defined in tail -f's man page. But nevertheless I have clarified my question.
    – d135-1r43
    Nov 7, 2012 at 12:14
  • 1
    Use less --follow-name see unix.stackexchange.com/a/74287/112522
    – djsutho
    Aug 28, 2015 at 0:52
  • Appears from the comments to answers that he wants a full-blown "less" that is running and operating on the "current version" of a certain large file (i.e. at the top)...
    – rogerdpack
    Apr 7, 2021 at 17:25

5 Answers 5

11

You can get this effect by issuing the F command (Shift+F) while viewing the file in less. To stop following and switch back to paging, press Ctrl+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,

tail -f -s 60 myfile

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:

while true; do ( sh -c 'sleep 600 && kill $PPID' & less myfile ); done

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:

touch killfile
while [ -f killfile]; do stuff; done

To stop doing stuff, rm the killfile in another shell.

4
  • This is only partially useful for my case. The watched file is very big the updated information at the top. It is no "scrolling" log file. Shift+F immediately scrolls to the end of the file...
    – d135-1r43
    Nov 6, 2012 at 14:04
  • You should edit your question to clarify. I edited my answer to answer this, too. Nov 7, 2012 at 2:10
  • watch -n 1 less <filename>
    – axel22
    Oct 8, 2019 at 8:58
  • appears watch -n 1 less <fiename> is the equivalent of watch -n 1 head -n40 <filename> (i.e. less piped in becomes same as cat, but watch shows you the topmost lines) FWIW...so it does get you the top lines anyway :)
    – rogerdpack
    Apr 7, 2021 at 17:31
2

You can do something similar in vim.

Start a server session of vim:

vim -R --servername refresh_session

Then in another console, monitor the file for updates and tell the vim session to reload the file as soon as it gets updated:

inotifywait -e close_write -m your_log_file | while read filename events; do
    vim --servername refresh_session --remote $filename
done

A few gotchas.

  • This will of course not work if your vim is not compiled with the clientserver feature in.
  • inoifywait will stop working when the file gets deleted. So I hope your file gets overwritten. It is possible to work around that, too, of course.

And if you want to have a more less-like experience, you can use the less macros to get your less key bindings in vim.

/usr/share/vim/vim73/macros/less.sh --servername refresh_session
0

You might try this command: tail -f file

0

Tail is your friend. "tail -f filename" will show you new lines when they appear.

Or if you are looking for changes made in the middle of the file, maybe run a script every few minutes to make a copy of the file to a temp location and do a diff on it?

3
  • It was quite clear even in the original post that there was a "new file". Therefore the file descriptor would have changed but tail -f would still read the old one. Apr 8, 2014 at 5:42
  • I don't think that's very fair. To be crystal clear, we need to look at the original, unedited post, because if you look at the date of my answer, that's the question I was answering. In that unedited question, my answer makes perfect sense. serverfault.com/posts/445899/revisions
    – David W
    Apr 8, 2014 at 11:29
  • I did check the revisions. Keywords "new file". Apr 8, 2014 at 15:49
0

I found an old, but very good "less" like program (a "pager"). It has a "R" command that refresh the file.

It's called "lv", You can install it on Ubuntu using:

sudo apt install lv

However, it seems to have no autoreload feature.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .