Is there a way to dynamically refresh the less command?

I like the watch command, but it has its limitations.

I’m curious to know whether I could mimic the functionality of watch with less. I’m mainly looking for the ability to scroll through my directory as it dynamically gets modified via a running script.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In less, you can type F (Shift+F) to keep reading at the end of a file (like tail -f); you can type :e and a file name to view a different file, but unfortunately, if you enter the name of the current file, less doesn’t reload the file. However there’s a trick to make it re-read the current file, suggested by sabgenton: type :e and 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 R to reload the (local) URL.

Method 2

Shift+F will make less similar to tailf. That is, it gets refreshed if more data is appended to the file.

Method 3

Simply type:

less +F filename

This emulates pressing “F” within the editor.

Method 4

man pages can be very informative. Don’t be intimidated by them. Among everything else, man less says you can use the R command to:

   R      Repaint the screen, discarding any buffered input.  Useful if the file is changing while it is being viewed.

(I realize this question is over 6 years old, but it comes up on google searches, so I’m not the only one that clicked the link to get here.)

Method 5

R for repaint does not always reload the file.[1]

A workaround that always reloads the file is to press hq, which will open the Help page, then quit. It has a side effect of forcing the file to reload.


[1] Here are some examples of situations that R do and do not reload:

  • > and >> changes: DO get reloaded
  • sed -i, gEdit, TextEdit: DO NOT get reloaded
  • On Linux, vi changes: DO get reloaded
  • On Mac, vi changes: DO NOT get reloaded

I believe the difference comes down to whether the inode changes (you can check with ls -i foo.txt). If the inode changes, then R will not work.

Method 6

I normally just type G to tail the output on a one-time basis. I find it especially helpful over a a network file system like CIFS.

Method 7

The “F” key when running less will do a “follow” similar to tail -f, but I’m not sure if that will achieve what you’re looking for here.

Method 8

You could pipe it to tail -f instead, it would result in you following the output. You’d be losing the ability to move (scroll) through your output though.

Method 9

You can use vim to read the file then add the following mapping to your .vimrc file and you can easily reload a file with ,r:

let mapleader = ","
nnoremap <leader>r :edit <CR>

Note if you edited the file already, vim will complain. Just change to
let mapleader = ","
nnoremap <leader>r :edit! <CR>

To ignore changes.

Method 10

I worked around this problem using a shell script that auto-reloads less on exit:

while true; do less -K file.txt || exit; done

With this script, I can hit q to reload the file, and CTRL+C to exit back to the bash shell. The CTRL+C behavior is enabled via the -K option. Your last search term will be preserved.

This can be further refactored by using the colon (:) to create an empty expression, via do :

while less -K file.txt; do : ; done

Drawbacks

The current viewing position will always be rest to line 0.

Practical Example using mintty

In my windows (GitBash) environment I set up a script that opens a new terminal window (mintty) for less-viewing a file:

lesswin() { mintty bash -c "while less -K "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e6c2a6">[email protected]</a>"; do : ; done;" & }

Method 11

I just found this thread like anyone else.
I would like to add the solution of when you are already at the end of the file, using ‘g’ followed by ‘G’ will force a refresh of the file.

I ended up making a macro button for this in my terminal program (SecureCRT).
The macro is simply ‘gG’.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
michealSmith07
3 months ago

That is a very nice post. I like this post.

1
0
Would love your thoughts, please comment.x
()
x