观看文件以进行更改

sha*_*nuo 7 linux shell command-line

我想观察文件xyz.txt的任何更改,并在发生更改时通过电子邮件发送给我整个文件.是否有一个Liner(或几行shell脚本)?

更新:

# 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
Run Code Online (Sandbox Code Playgroud)

现在,如果有人可以展示如何在shell脚本或1命令中绑定这两行.

igo*_*gor 22

你可以用inotifywait.它等待对文件的更改,然后执行命令(例如,msmtp在您的情况下).


Mar*_*row 8

您应该查看可以查看文件或目录并报告更改的inotify.


Pau*_*ce. 4

尝试一下:

find /etc/ -name my.cnf -mtime 0 -exec sh -c 'cat {} | mail -E -s "file changed" shantanu@company.com' \;
Run Code Online (Sandbox Code Playgroud)

-E阻止mail它发送带有空正文的消息的选项(如果不返回任何内容并且不输出任何内容,就会出现这种情况findcat