use*_*451 3 linux debian file-transfer
我有目录a和目录b。目录 a 具有定期复制到其中的新文件和文件夹。我想监视文件夹 a 中的那些新文件并自动将它们复制到文件夹 b。不幸的是,由于我之前在目录 b 中设置的一些组织脚本使我无法将 rsync 用于这些目的,因为目的地的文件夹结构很可能在 rsync 运行之间差异太大。
我可以使用任何类型的替代设置吗?
另一种方法是使用inotify:
安装inotify-tools包
sudo apt-get install inotify-tools
Run Code Online (Sandbox Code Playgroud)
编写一个小脚本,用于inotifywatch检查文件夹的更改并将任何新文件移动到目标目录:
#!/usr/bin/env bash
## The target and source can contain spaces as
## long as they are quoted.
target="/path/to/target dir"
source="/path to/source/dir";
while true; do
## Watch for new files, the grep will return true if a file has
## been copied, modified or created.
inotifywatch -e modify -e create -e moved_to -t 1 "$source" 2>/dev/null |
grep total &&
## The -u option to cp causes it to only copy files
## that are newer in $source than in $target. Any files
## not present in $target will be copied.
cp -vu "$source"/* "$target"/
done
Run Code Online (Sandbox Code Playgroud)
将该脚本保存在您的$PATH文件中并使其可执行,例如:
chmod 744 /usr/bin/watch_dir.sh
Run Code Online (Sandbox Code Playgroud)
每次机器重新启动时都运行它,创建一个 crontab(crontab -e如@MariusMatutiae 的回答中所述)并将此行添加到其中:
@reboot /usr/bin/watch_dir.sh
Run Code Online (Sandbox Code Playgroud)
现在,每次重新启动时,都会自动监视该目录,并将新文件从源复制到目标。
| 归档时间: |
|
| 查看次数: |
18612 次 |
| 最近记录: |