Rsync

rsync 不能排除相關文件?

  • July 9, 2020

我知道 rsync 中的 exclude 選項只能接受相對路徑,所以我雖然可以使用realpath --relative.

$ls /usr/local/bin
cppman  demangle  e  fusuma  rem  triangle
$realpath --relative-to=$PWD /usr/local/bin/cppman
../../usr/local/bin/cppman
$rsync -va --delete --exclude=$(realpath --relative-to=$PWD /usr/local/bin/cppman) /usr/local/bin /home/user1/Desktop/transport/
created directory /home/shepherd/Desktop/transport
bin/
bin/cppman
bin/demangle
bin/e
bin/fusuma
bin/rem
bin/triangle

sent 26,689 bytes  received 189 bytes  53,756.00 bytes/sec
total size is 26,215  speedup is 0.98

如您所見,儘管是相對文件,但並未排除cppman該*文件。*為什麼?

排除路徑相對於您的源(“傳輸的根”)開始,而不是您目前的工作目錄。由於bin是要傳輸的目錄,因此路徑為/bin/cppman

rsync -va --delete --exclude=/bin/cppman /usr/local/bin /home/user1/Desktop/transport/

或未錨定(沒有前導/):

rsync -va --delete --exclude=bin/cppman /usr/local/bin /home/user1/Desktop/transport/

如果您有兩個文件/usr/local/bin/cppman/usr/local/bin/foo/bin/cppman,則未錨定版本將排除這兩個文件,而第一個命令將排除第一個文件。

引用自:https://unix.stackexchange.com/questions/597478