比较两个目录结构并删除目标位置中的多余文件和目录的最佳方法是什么?
我正在开发一个小型networking照片库应用程序。 用户使用FTP添加和删除图像。 我写的networking图库软件可以即时创build新的缩略图,但不会处理删除操作。 我想要做的是安排一个命令/ bash脚本以预定义的时间间隔来处理这个问题。
原始图像存储在/home/gallery/images/并使用子目录在相册中进行组织。 缩略图caching在/home/gallery/thumbs/ ,使用与images目录相同的目录结构和文件名。
我已经尝试使用以下来实现这一点:
rsync -r --delete --ignore-existing /home/gallery/images /home/gallery/thumbs
如果所有的缩略图都已经被caching了,那么这样做可以正常工作,但是不能保证这种情况会发生,当这种情况发生时,缩略图目录会复制原始的全尺寸图像。
我怎样才能最好地实现我想要做的?
我不认为rsync是最好的方法。 我会使用一个bash单线程如下所示:
$ cd /home/gallery/thumbs && find . -type f | while read file;do if [ ! -f "../images/$file" ];then echo "$file";fi;done
如果这一行代码生成正确的文件列表,则可以修改它以运行rm命令而不是echo命令。
你也需要 – --existing也存在:
rsync -r --delete --existing --ignore-existing /home/gallery/images /home/gallery/thumbs
从手册:
--existing, --ignore-non-existing This tells rsync to skip creating files (including directories) that do not exist yet on the destination. If this option is combined with the --ignore-existing option, no files will be updated (which can be useful if all you want to do is delete extraneous files).