30

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.

Kit
  • 1,043
  • 3
  • 10
  • 13
  • 1
    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
  • google -> py inotify "Pyinotify is a Python module for monitoring filesystems changes. Pyinotify relies on a Linux Kernel feature (merged in kernel 2.6.13) called inotify. inotify is an event-driven notifier, its notifications are exported from kernel space to user space through three system calls. pyinotify binds these system calls and provides an implementation on top of them offering a generic and abstract way to manipulate those functionalities." – jirib Nov 15 '13 at 14:32
  • 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: http://en.wikipedia.org/wiki/Inotify – slm Nov 15 '13 at 14:40

6 Answers6

41

This should show you the file once per second:

watch -n 1 cat file
21
tail -f /var/log/syslog

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

Joe C.
  • 219
  • 2
  • 2
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
slm
  • 369,824
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
    
terdon
  • 242,166
  • 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.

0

If you want to see the new lines being appended to a file that has more than one page I recommend to use

watch -n 1 tail -n 50 log.txt

watch -n 1 will show updates at every 1 second and tail -n 50 will show last 50 lines and in combination this will result in a automatic display of last 50 lines of log.txt that refreshes every 1 second