Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 50 down vote accepted

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.
share|improve this answer
    
For me :e randomnamejkdlfjldf fails to load the non-existent filename then reloads back to the current file :D – sabgenton Mar 18 at 10:36
    
@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 at 16:14
    
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 Apr 19 at 9:01
    
@CMCDragonkai No, less +F only watches for appended content. Use :e nonexistentfile to re-read completely changed input. – Gilles Apr 19 at 11:08
    
Is there a command line flag for that? – CMCDragonkai Apr 19 at 11:18

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

I normally use shift+G,shift+G to tail the output, or file, on a one-time basis. I find it especially helpful over a a network file system like CIFS.

share|improve this answer

Simply type:

less +F filename

This emulates pressing "F" within the editor.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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