130

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.

  • I am looking for tail -f foo.log | grep bar, and to be able to dynamically change the grep command with restarting the tail/grep pipeline. – Alexander Mills Sep 13 '18 at 23:56

11 Answers 11

110

In less, you can type 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.
  • 6
    For me :e randomnamejkdlfjldf fails to load the non-existent filename then reloads back to the current file :D – sabgenton Mar 18 '16 at 10:36
  • 1
    @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). – Gilles Mar 18 '16 at 16:14
  • 1
    @CMCDragonkai No, less +F only watches for appended content. Use :e nonexistentfile to re-read completely changed input. – Gilles Apr 19 '16 at 11:08
  • 1
    Is there a command line flag for that? – CMCDragonkai Apr 19 '16 at 11:18
  • 3
    @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 Apr 20 '17 at 9:53
63

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

  • 7
    Note that this only works for appends. If lines are removed or edited in-place, less won't show those changes. – Nathaniel M. Beaver Nov 30 '16 at 1:26
  • How do I stop following mode and re-enter scrollable mode? – Tom Hale Feb 9 '17 at 12:18
  • 1
    <kbd>Ctrl+C</kbd> – balki Feb 9 '17 at 20:41
  • 1
    balki My experience is that this kills the command piping the data if you are using $COMMAND | less. I wonder if there is a nice way around this? You can do stuff like $COMMAND > /tmp/file & less /tmp/file but that's not very nice. – Att Righ Jul 27 '18 at 11:13
  • @AttRigh I never killed my command (however i read logs of running commands this way, i don't pipe into less, technically) – jena Jan 11 at 13:48
37

Simply type:

less +F filename

This emulates pressing "F" within the editor.

  • 1
    Thanks; added the alias alias check="less +F" to my .bashrc. – Luke Davis Jul 27 '17 at 21:31
19

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.)

  • 6
    This repaints the screen in case it was disrupted by output from another program. It doesn't consistently reload the file. I haven't dug deeply into the code to see what it does, experimentally short files don't get reloaded. I am definitely not intimidated by man pages and I say this description is poorly worded: “repaint” does not convey that input is re-read, and the actual behavior does not quite fit “useful if the file is changing” (maybe it's a bug? I don't know since I don't know what the command is supposed to do).. – Gilles Apr 20 '17 at 9:51
  • I'm no expert, and haven't read less's source code either, but what it does is forget what it already knows about the file & reloads it, refreshing its output based on the input it reloads. The question reads "Is there a way to dynamically refresh the less command?" If I had read the question's explanation more thoroughly before answering, I might not have answered, since they go on to say they want "to scroll through my directory as it dynamically gets modified via a running script." less wouldn't be the right command for that. – destenson Apr 21 '17 at 3:58
  • Works fine for me, even when using files 3 bytes long. I can't imagine why it wouldn't work for larger files too. – Addison Mar 22 '18 at 3:51
  • 1
    Great! For me it works for both short and long files. I just noticed two possible problems: 1. less did not reopen the file using its filename - i.e. when the inode of the file changed (like in overwriting the file using mv) then the old file content stayed (probably the old inode was reread) --- 2. The first displayed line probably starts at the same byte offset as in the original file content. So when the line lengths change the first displayed line could be incomplete and the displayed lines could move up / down. – pabouk Sep 13 '18 at 11:27
  • R doesn't always work. See unix.stackexchange.com/questions/4351/… – wisbucky Apr 17 at 19:18
5

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.

5

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.

  • This is more of an on-demand refresh of the file opened with less. In addition to jumping to the end of the file, it also reloads the file if it has changed. – jorb Jun 6 '17 at 13:23
  • 1
    Thanks. First time it didn't refresh for me, but now it seems to work properly. In the man it's not written that it refreshes the file. man less: 'G or > or ESC-> Go to line N in the file, default the end of the file.' I delete my old wrong comments here, sorry for them. – Yaroslav Nikitenko Jun 8 '17 at 13:56
  • 1
    BTW, what do you call a 'one-time basis'? The maintainer of less, Mark Nudelman, replied the following: "The G command does not normally force a re-read of the file like the R command does. It may seem do that if the end of the file has not yet been read when G is invoked, so that jumping to the end of the file must read the data there for the first time. For example, if you jump to the end with G, then jump back to the beginning with 1G, then another program modifies the data at the end of the file (without changing the length), " (cont) – Yaroslav Nikitenko Jun 12 '17 at 20:14
  • (cont) "then you jump to the end again with G, you will not see the modified data. But if you don't jump to the end and back to the beginning, then the first time you execute the G command it must read the data there for the first time, and of course it will see any modifications that have happened since less was first invoked. " – Yaroslav Nikitenko Jun 12 '17 at 20:14
  • In this case I mentioned it being a one-time refresh as opposed to a dynamic refresh, which is part of what the original question is asking. – jorb Jun 12 '17 at 20:31
1

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.

  • They'll have to pry vim from your cold, dead hands. The context of the question is about less, but as someone with monomaniacal tendencies myself, I sympathise! – Benjamin R Oct 26 '18 at 16:05
1

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.

0

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.

0

If you're not averse to using a browser you could launch the Algernon web server with this command:

algernon -a -t /directory/name

A list of files will then be displayed, and auto-refreshed, at http://localhost:3000/

  • Wrong thread?.. – Tomasz Jun 5 '17 at 23:45
  • It seems to be a way to watch a directory, although it's a long way from less – Jeff Schaller Jun 6 '17 at 0:17
0

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'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.