如何在文件或目录更改时运行 shell 脚本? [英] How to run a shell script when a file or directory changes?
本文介绍了如何在文件或目录更改时运行 shell 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在特定文件或目录更改时运行 shell 脚本.
I want to run a shell script when a specific file or directory changes.
我怎样才能轻松做到这一点?
How can I easily do that?
推荐答案
使用 inotify-tools.
链接的 Github 页面有许多示例;这是其中之一.
The linked Github page has a number of examples; here is one of them.
#!/bin/sh
cwd=$(pwd)
inotifywait -mr
--timefmt '%d/%m/%y %H:%M' --format '%T %w %f'
-e close_write /tmp/test |
while read -r date time dir file; do
changed_abs=${dir}${file}
changed_rel=${changed_abs#"$cwd"/}
rsync --progress --relative -vrae 'ssh -p 22' "$changed_rel"
usernam@example.com:/backup/root/dir &&
echo "At ${time} on ${date}, file $changed_abs was backed up via rsync" >&2
done
这篇关于如何在文件或目录更改时运行 shell 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文