我在同一台计算机上的不同路径中有两个文件夹:A和B。当我将任何新文件添加到文件夹A时,我想将其自动复制到文件夹B。
/auto/std1/nat1/A
/auto/std2/nat2/B
cp -r A B
但我想这个过程在后台为每个新文件和文件夹中自动运行A
到B
。
在复制文件时,我希望对某些文件类型执行特定的操作,例如:当我zip
在folder中有一个文件时A
,我希望将其自动unzip
复制到Folder中的该文件中B
。
这是在CentOS 7系统上。
对于您的红利问题,请在下面rsync
提供的Shell脚本中的命令下方添加以下行。我在评论中写了这个,但是我将在这里正式将其添加到我的答案中:
find /auto/std2/nat2/B -name '*.zip' -exec sh -c 'unzip -d `dirname {}` {}' ';'
这将解压缩处理是通过复制的所有zip文件rsync
从文件夹/auto/std2/nat2/A
到/auto/std2/nat2/B
如果已rsync
安装,为什么不只是cron它并rsync
管理文件镜像?
myrsyncscript.sh
chmod 700 myrsyncscript.sh
#!/bin/sh
LOCKFILE=/tmp/.hiddenrsync.lock
if [ -e $LOCKFILE ]
then
echo "Lockfile exists, process currently running."
echo "If no processes exist, remove $LOCKFILE to clear."
echo "Exiting..."
# mailx -s "Rsync Lock - Lock File found" [email protected] <<+
#Lockfile exists, process currently running.
#If no processes exist, remove $LOCKFILE to clear.
#+
exit
fi
touch $LOCKFILE
timestamp=`date +%Y-%m-%d::%H:%M:%s`
echo "Process started at: $timestamp" >> $LOCKFILE
## Run Rsync if no Lockfile
rsync -a --no-compress /auto/std1/nat1/A /auto/std2/nat2/B
echo "Task Finished, removing lock file now at `date +%Y-%m-%d::%H:%M:%s`"
rm $LOCKFILE
选项明细:
-a is for archive, which preserves ownership, permissions etc.
--no-compress as there's no lack of bandwidth between local devices
您可能会考虑的其他选项man rsync
:
--ignore-existing
跳过更新接收器上存在的文件
- 更新
这将强制rsync跳过目标上存在的任何文件,并且修改后的时间比源文件新。(如果现有目标文件的修改时间与源文件的修改时间相等,则如果大小不同,它将被更新。)请注意,这不会影响符号链接或其他特殊文件的复制。同样,无论对象的日期是什么,发送方和接收方之间文件格式的差异始终被认为对于更新足够重要。换句话说,如果源具有目录,而目标具有文件所在的目录,则无论时间戳如何,都会进行传输。
此选项是一个传输规则,而不是排除规则,因此它不会影响进入文件列表的数据,因此也不会影响删除。它只是限制了接收者请求传输的文件。
像这样将其添加到cron中,并将频率设置为您最喜欢的频率:
crontab -e
并添加以下内容:### Every 5 minutes
*/5 * * * * /path/to/my/script/myrsyncscript.sh > /path/to/my/logfile 2>&1
# * * * * * command to execute
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句