Is there a way to dynamically refresh the less command?
I like the
watchcommand, but it has its limitations.
I'm curious to know whether I could mimic the functionality of
less. I'm mainly looking for the ability to scroll through my directory as it dynamically gets modified via a running script.
less, you can type
Fto keep reading at the end of a file (like
tail -f); you can type
:eand a file name to view a different file, but unfortunately, if you enter the name of the current file,
lessdoesn't reload the file. However there's a trick to make it re-read the current file, suggested by sabgenton: type
:eand enter the name of a non-existent file; that causes less to display an error message and then reload the current file.
If you're looking for an alternative to
watch ls, here are a few:
- Modern file managers (e.g. Nautilus, Thunar, Konqueror, Dolphin, Finder) refresh views in real time.
- Emacs doesn't have real-time refresh, but with
auto-revert-mode, it will reload the file or directory every 5 seconds (the delay is configurable).
- Although w3m is primarily a web browser, it makes a passable directory and text file viewer. Press
Rto reload the (local) URL.
For me :e randomnamejkdlfjldf fails to load the non-existent filename then reloads back to the current file :D
@sabgenton Thanks, I didn't know that. That's a great trick. I added it to my answer, but you could post it as an answer of your own (which I hope Zaid would accept).
What if the file contents change but changes aren't necessarily appended. As in the file contents may be replaced completely, or partially. Does `less +F` deal with such a thing?
@CMCDragonkai No, `less +F` only watches for appended content. Use `:e nonexistentfile` to re-read completely changed input.
@CMCDragonkai No. If you want automatic refresh, I think you need a fancier tool like Emacs with autorevert mode, or else send the `:e` automatically (like http://askubuntu.com/questions/355301/terminal-web-browser-page-reload).
The answer from @destenson should also be added/linked to https://unix.stackexchange.com/a/330009/13676 This is the real answer, since it turns out that less also has a 'R' command to 'Refresh' the file.
@PaulWagland No, `R` doesn't reliably refresh the file. It refreshes the screen, and wipes some input buffers, but it doesn't actually reload data from the file in all circumstances (only if the file is large? I didn't dig deeply into the code).
@Gilles Ah. OK, the test that I was doing was indeed with a large (15Gb!!) file, so that might explain why I always saw the refresh. Thanks for the update.
I wish I could give you a +1 for every time I forget about this and find myself here again. :D
This solution will work when `R` doesn't: https://unix.stackexchange.com/questions/4351/is-there-a-way-to-dynamically-refresh-the-less-command/445633#445633