I want to watch any changes to a file xyz.txt and email me the entire file whenever there is a change. Is there One Liner (or a few lines shell script) for this?
Update:
# Check if my.cnf has been changed in the last 24 hours
# if yes, as in the following case, simply send the file
# if it has not been changed in the last 24 hours, do nothing.
# find /etc/ -name my.cnf -mtime 0
/etc/my.cnf
# cat /etc/my.cnf | mail shantanu@company.com
Now if someone can show how to bind these two lines in a shell script or in 1 command.
You should look into inotify which can watch a file or directory and report changes.
As I've learnt from another question over at superuser (all credit due there):
This watches a given file (or files) for changes and runs the command whenever (the instant) it changes. So you could do it like:
(PS. Credit to Denis's answer for the mail command)
inotify-hookable is a perl script that is quite easy to use for this purpose. For example,
-f for the file to watch -c for the command to run
I had it watching a file in on a remote computer too, but inotify-hookable finished when the watched file was deleted prior to being updated.
I installed it from Debian. CPAN link: https://metacpan.org/pod/App::Inotify::Hookable
You could use
inotifywait
. It waits for changes to a file, and then executes a command (e.g. something likemsmtp
in your case).Give this a try:
The
-E
option tomail
prevents it from sending messages with empty bodies (as would be the case iffind
returns nothing andcat
outputs nothing.