Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display text file and update when the file contents change

Tags:

bash

I want to have a window open that is just the content of the file file.txt. I do not care which command I use to open it, nano, cat, vi or whatever. I want it to update every time it changes, so if I open it in another window, edit it and save it, the file in the original window will update, kind of like screen. Any suggestions?

I have already tried just having it open a new cat file.txt command every few seconds, but it is very unreliable.

like image 404
Patrick Cook Avatar asked Aug 30 '15 18:08

Patrick Cook


People also ask

How do you display the contents of a text file?

Use the command line to navigate to the Desktop, and then type cat myFile. txt . This will print the contents of the file to your command line. This is the same idea as using the GUI to double-click on the text file to see its contents.

How do I display the content of a file as it grows Linux?

Cat. The simplest way to view text files in Linux is the cat command. It displays the complete contents in the command line without using inputs to scroll through it. Here is an example of using the cat command to view the Linux version by displaying the contents of the /proc/version file.

How do you update a text document?

In the Actions ribbon, select File & Folder > Text File Update. In the File field, use the ellipsis (...) to navigate to the text file you want to update. In the Search For field, enter the text which you want to update. The search will look for the exact sequence of words entered.


4 Answers

Use

watch cat file.txt

That should update when any changes occur, also try

man watch

To see what options it has so you can update the frequency of the updates.

like image 66
Dave Mackintosh Avatar answered Oct 17 '22 23:10

Dave Mackintosh


The low-tech solution in case watch is not available:

 while sleep 1; do tput clear; cat file.txt; done

(lets you easily adapt the checking interval).

like image 25
Jens Avatar answered Oct 17 '22 22:10

Jens


On Linux, you can use the inotifywait command like so:

#!/bin/sh
while inotifywait --event modify file.txt; do
    tput clear
    cat file.txt
done

which is a modified version of Example 2 in the man page. This has the great advantage of doing absolutely nothing until file.txt is modified. The answers suggesting polling have the problems that polling always does: it will waste time when nothing has changed, and it will fail to catch changes until the polling interval has ended.

like image 43
msw Avatar answered Oct 17 '22 22:10

msw


If you are on macOS you probably don't have watch installed per standard. You could go for the low-tech solution - which is elegant anyway, but you can also revert to home-brew.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

You might have to give it (once!) your (administration) password. After the installation finishes, you should always check the system:

brew doctor

There should be a message along the lines:

Your system is ready to brew.

Now you are ready to install watch:

brew install watch

Once that is finished, you are ready for the solution by @dave-mackintosh:

watch cat file.txt

like image 21
Sander W. van der Laan Avatar answered Oct 17 '22 22:10

Sander W. van der Laan