创建后立即将文件自动复制到目的地

M.N*_*hru 5 shell-script file-copy

有没有办法在创建文件后立即在Unix中复制文件?我有一个位置,文件将在不同的时间创建。如何使用 shell 脚本自动将文件复制到 Unix 中的其他位置?

PSk*_*cik 5

inotifywait -e close_write --format "%f" --monitor . |
  while IFS= read -r line
  do
    #cp "$line" destination/ #this would not replicate directories
    tar cf - "$line" | tar -C destination/ xf - #this should replicate directories
  done
Run Code Online (Sandbox Code Playgroud)

应该可以解决问题,除非您的目录中的文件数(我.用于当前目录)超过系统对 inotify 手表的限制(fs.inotify.max_user_watches,如 coteyr 所述)。

  • 同样重要的是要注意文件监视限制 (fs.inotify.max_user_watches) 我一直使用 inotify 达到该限制。 (2认同)