2

I'd like to watch a file for changes that might occur. Normally, using tail -f file or watch -d cat file would be fine for this. However, the file I am monitoring is too large to fit on one screen, and the changes don't necessarily occur at a particular location (e.g., the end).

How can I watch for changes? Ideally, I'd like something like watch -d cat file that scrolls so that at least one change is visible on screen.

If you're wondering what this is for, I'm using lithium to minimize a large file, and I like to watch its progress since the minimization process often reveals hints about the underlying bug.

1
  • You could watch a diff (if you constantly have access to a copy of the original file) instead of watching the file?
    – Celada
    Commented Jan 21, 2017 at 17:54

2 Answers 2

2
watch=/path/to/file
tmp="$watch".$$
cp "$watch" "$tmp".1
while true; do
    clear
    cp "$watch" "$tmp".2
    diff -u "$tmp".1 "$tmp".2
    mv "$tmp".2 "$tmp".1
    sleep 10
done

If you are concerned about the space and or time needed for those copies of the whole file, you have to realize that there is actually no way around this to implement your requirements. watch -d also has to keep the last output around to compare it against the current.

1

Polling the file with an infinite loop is a bad idea. My advice is to install nodejs and use fs.watchFile.

fs.watchFile('message.text', (curr, prev) => {
  console.log(`the current mtime is: ${curr.mtime}`);
  console.log(`the previous mtime was: ${prev.mtime}`);
});

If you want a one line command for the terminal do something like this.

node -e "fs.watchFile('message.text', (curr, prev) => {
  console.log(`the current mtime is: ${curr.mtime}`);
  console.log(`the previous mtime was: ${prev.mtime}`);
});"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .