使用 rsync 删除重复文件

cod*_*dme 5 linux ssh centos rsync

这是事情,

我的服务器上有一个 50 GB 大小的文件夹,其中包含超过 60000 个文件。我使用rsync将它传输到镜像服务器,几乎一半的文件已经传输。现在我想删除主服务器上传输的文件。

这可以用 rsync 完成吗?我确实阅读了帮助并找到了 --delete 选项,但这些文件非常重要,所以我想获得专家意见,谢谢。

Flo*_*aus 5

rsync(用 3.0.9 版检查)有一个选项叫做--remove-source-files它做它所说的。如果您只想删除传输的文件而不传输尚未传输的其他文件,则需要额外使用选项`--existing``。

不幸的是,即使使用了选项,rsync 似乎也不会输出它要删除的文件--verbose --itemize-changes --stats

例子

# create source and target dirs
mkdir /tmp/source
mkdir /tmp/target
# create a test file in source
touch /tmp/source/test
# rsync source and target
rsync --archive --itemize-changes --verbose --stats /tmp/source/ /tmp/target
# verify that test has been copied to target
[ -f /tmp/target/test ] && echo "Found" || echo "Not found"
# create another file in source
touch /tmp/source/test2
# delete files on source which are already existing on target
rsync --archive --itemize-changes --verbose --stats --remove-source-files --existing /tmp/source/ /tmp/target
# verify that test has been deleted on source
[ -f /tmp/source/test ] && echo "Found" || echo "Not found"
# verify that test2 still exists on source and was not transferred to target
[ -f /tmp/source/test2 ] && echo "Found" || echo "Not found"
[ -f /tmp/target/test2 ] && echo "Found" || echo "Not found"
Run Code Online (Sandbox Code Playgroud)