Take the 2-minute tour ×
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.

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.

share|improve this question
    
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. –  Jiri Xichtkniha 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.......? –  Jiri Xichtkniha 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." –  Jiri Xichtkniha Nov 15 '13 at 14:32

3 Answers 3

up vote 5 down vote accepted

This should show you the file once per second:

watch -n 1 cat file
share|improve this answer
    
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

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
share|improve this answer

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 total && 
      echo "$(date;cat kk)"; 
    done
    
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.