I have to use watch in order to write in a file (doc.txt) the differences between a new version of a file and an older.

I tried watch -t -d -n 10 "cat myfile.txt | tee doc.txt" but all myfile.txt's text is written in the terminal with the command cat and differences are only hightlighted. And in doc.txt, there is the same content as in myfile.txt.

Any ideas?

1 Answer 1


man of watch says

watch - execute a program periodically, showing output fullscreen


 -n, --interval seconds
              Specify  update  interval.   The command will not allow quicker
              than 0.1 second interval, in which the smaller values are  con‐
              verted. Both '.' and ',' work for any locales.

 -d, --differences [permanent]
              Highlight the differences between successive  updates.   Option
              will read optional argument that changes highlight to be perma‐
              nent, allowing to see what has  changed  at  least  once  since
              first iteration.       
 -t, --no-title
              Turn off the header showing the interval, command, and  current
              time  at the top of the display, as well as the following blank

man of cat says

 cat - concatenate files and print on the standard output

man of tee says

tee - read from standard input and write to standard output and files

This might work

Watch for differences between 2 version of doc.txt every 10 seconds and write it to a file doc.txt

watch -t -d doc.txt -n 10 | tee new.txt

  • The problem is that watch only takes a command ( so just doc.txt isn't sufficient, it's why I used cat ), and I watched the differences bewteen the new cat myfile.txt and the last.
    – Sarah
    May 13, 2020 at 16:15
  • I fund : watch -t -n 10 "wdiff last.txt new.txt | grep -Poz '\[-(?s).*\n' | tee test.txt && cat last.txt > new.txt"
    – Sarah
    May 13, 2020 at 16:30

You must log in to answer this question.

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