I have the following script running every 3 minutes with cron:

#!/bin/bash
cd ~/Documents/Fetch/
wget remotehost/data.txt --output-document=tmp
cp tmp data.txt
rm tmp

To fetch a self-generated php log.

I want to check if any changes have been made to the file (with a 3 minute timespan). So in a terminal on my second screen, i'm running tail -f ~/Documents/Fetch/

Now, if no changes are made to the data.txt file, tail just outputs the same log over and over again, separated by tail: /home/midas/Documents/Fetch/data.txt: file truncated.

is there a way to tell tail to completely clear the terminal screen before showing the output when the file is truncated?

I basically want this to happen:

  1. tail prints initial content
  2. tail notices the file is truncated > clear
  3. tail prints the content it sees after truncation of the file.

The man tail page did not seem to give me this option.

Does anyone have experience with this?

I know it kinda beats the purpose of tail itself, but I can imagine a few situations where this would be handy.

  • Like with clear && tail -f $filename? – Konstantinos Jun 23 '15 at 21:52
  • No, I actually want clear to be ran everytime tail -f prints new content. (As I'm completely copying the new logfile over the old one). clear && tail -f $filename clears everything, and then it starts running tail -f – Midas Jun 23 '15 at 22:01
  • Sorry I'm too tired and I'll go to sleep. watch tail $filename may also help. I am too sleepy to understand the question. Will check it tomorrow. :) – Konstantinos Jun 23 '15 at 22:07
  • I don't think watch and tail go well together. It doesn't quite do what I hope it would anyways. I might be a bit unclear, I'll see if I can clarify further :) Thanks for the effort! – Midas Jun 23 '15 at 22:11
  • Chances are that you are indeed clear, but I'm just too tired. Will check tomorrow. – Konstantinos Jun 23 '15 at 22:14
up vote 3 down vote accepted

If your content is relatively short (less than one terminal screen's worth, for whatever number of lines in your terminal), then you can run the tail repeatedly

watch -n 30 'tail /path/to/filename 2>&1'

This runs the tail every 30 seconds. You might want to use a different interval (the default is 2 seconds, but since you have a generation granularity of 3 minutes there's no a lot of point in having it that low).

For subtle changes the -d flag can also be useful to highlight differences.

  • Thanks! I wasn't too familiar with watch but with these params it indeed does what I hoped it would! – Midas Jun 23 '15 at 22:22
  • @Midas, actually thinking about it a little more, cat /path/to/filename instead of tail /path/to/filename should be equally effective – roaima Jun 23 '15 at 22:25
  • 1
    @ roaima You're right, it's actually even better as it'll show the whole content and with the -d flag on watch it'll highlight the changes. Thx for the tip! – Midas Jun 23 '15 at 22:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.