首頁·終端機命令

rsync

檔案與目錄

用法

rsync -av [source]/ [dest]/

只傳兩棵目錄樹之間的差異,本機或經 ssh 都行;來源路徑結尾的斜線表示它的內容,不寫就會多套一層目錄。

常用選項

選項含義
-aArchive: recursive, keeping permissions, times and symlinks
-vList what is transferred
-zCompress while it travels
-PShow progress and keep partial files so a transfer can resume
--deleteRemove files in the destination that are gone from the source
-nDry run: print what would happen and change nothing
--excludeSkip paths matching a pattern
-eChoose the remote shell, as in -e "ssh -p 2222"

範例

rsync -avP big.iso server:/data/

Copies with progress, and can resume later.

rsync -av --delete site/ /backup/site/

Makes the backup an exact mirror.

rsync -avn src/ dst/

Dry run first; nothing is written.

搬移、刪除和尋找。有些命令不會再問一次,所以順手加 -i 是在救自己。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. rsync 是做什麼的?

只傳兩棵目錄樹之間的差異,本機或經 ssh 都行;來源路徑結尾的斜線表示它的內容,不寫就會多套一層目錄。

Q. 怎麼寫?

rsync -av [source]/ [dest]/ —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 8 個,完整列表在 man rsync。這條命令屬於檔案與目錄。

相關命令

man 手冊: man rsync