12

How do I open a text file in a terminal with instant auto-refresh every time it is changed?

I've looked at vim with :set autoread, but it requires some elementary input (such as a keypress inside vim) to trigger the refresh.

I want the auto-refresh to be hands-free. Is there some hack to do this?

I'm using Crunchbang 11, but I'm quite comfortable with the terminal.

  • What is that good for? What is this file about? If it is a log, it usually has new lines added to the bottom, then you could just use tail -f $file. – jirib Nov 15 '13 at 13:48
  • Specifically, I am writing a Python script which writes out another text file, not just add new lines at the bottom. I'd like to monitor that text file. – Kit Nov 15 '13 at 13:52
  • inotify.......? – jirib Nov 15 '13 at 13:54
  • I'm not familiar with inotify. Looking it up, it's an API that I need to access with C programming, which I'm not inclined to do anytime soon. I'm looking for a shell command solution. – Kit Nov 15 '13 at 13:57
  • 1
    There is a suite of tools you can use from the command line called, typically, inotify-tools. These use a feature built into the Kernel called inotify. Any time a file is acted upon an event is sent via inotify that you can act on. Many prog. langs. such as Python, Perl, etc. have libraries that wrap the Inotify API. See the Wikipedia pg: en.wikipedia.org/wiki/Inotify – slm Nov 15 '13 at 14:40
22

This should show you the file once per second:

watch -n 1 cat file
  • 1
    This is what I'm looking for. Thanks! I extended it further with watch -tpcn 1 pygmentize -g filename – Kit Nov 15 '13 at 14:41
  • @Kit careful with watch -g, it doesn't work as you would expect. – terdon Nov 15 '13 at 14:44
  • @terdon, the -g switch is actually for pygmentize. Anyway, thanks for the warning. – Kit Nov 15 '13 at 15:08
  • If the file content is over one page, using this, you cannot scroll the screen to check the rest of the content of the file. – CodyChan Jan 18 '18 at 2:56
  • 1
    Why not just watch -n 1 tail file? – Varun Jun 1 '18 at 13:14
6
tail -f /var/log/syslog

Shows the syslog updates as they are added to the file.

  • This shows lines as they are added correct? OP needs a full refresh. – Sean256 Aug 7 at 20:51
3

I would use watch as the other answer suggests but just to show you how one can approach a seemingly complicated problem using the building blocks provided by a shell such as Unix; a while loop can be a simple way to perform your looping.

$ while [ 1 ]; do clear; date; cat <afile>; sleep 1 ;done

Example

$ while [ 1 ]; do clear; date; cat sample.txt; sleep 1 ;done
Fri Nov 15 09:17:39 EST 2013
1
2
3
4
5

The screen clears and then after a second, this gets displayed:

Fri Nov 15 09:17:40 EST 2013
1
2
3
4
5
2

As suggested in the comments, you could also use inotify though it is overkill. By far the simplest is to use watch. Here's one way to do it with inotify:

  1. Install the inotify-tools package

    sudo apt-get install inotify-tools
    
  2. Use inotifywatch to check your file for changes. Run it in a loop and cat the file if a change is detected (that's why I grep for the string total):

    while true; do 
      inotifywatch -e modify -t 1 kk 2>/dev/null | grep -q total && 
      echo "$(date;cat kk)"; 
    done
    
  • Passing -q (or --quiet) to grep will silence the matched line with "total". – FichteFoll Feb 25 '18 at 18:01
  • @FichteFoll good point (please disregard my previous comment if you saw it before I deleted, I was being slow). Thanks, edited. – terdon Feb 25 '18 at 18:06
0

watch(1) is a really useful tool. [1]

I would not recommend using cat though.

Instead, use head or tail for whether you need to see the beginning or the end of the file.

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.