
每天很多很多次,我發現自己想要將一到兩個檔案從本機電腦複製~/src/some/path/or/other
到測試主機,並分別複製到本機電腦和測試主機。分別在 ~/src 和 /srv/www 之後的兩個路徑是相同的——儘管顯然我通常會省略我目前使用的 scp 的第二個參數上的檔案名稱。~/src/some/different/path
/srv/www/some/path/or/other
/srv/www/some/different/path
問題是,我經常完全使用 scp 呼叫並將文件複製到錯誤的目錄,或者覆蓋不同的文件,因為 Tab Complete 設法智勝我。我可以透過 ssh 將整個本地樹同步到伺服器,但有時我還不想上傳整個內容——只是一個特定的檔案。
我想我想要的是簡單的 CLI 工具,它可以讓我做(也許需要一點配置)道德上相當於:
jkg5150@dev-laptop:~/src/myproject$ funkytool path/to/file another/path/another-file mydevhost.mycompany.com
……並已file
複製到mydevhost.mycompany.com:/srv/www/myproject/path/to/
,another-file
複製到mydevhost.mycompany.com:/srv/www/myproject/another/path/
。
當然,我缺少一個技巧——或者我應該寫一個?
答案1
將 rsync 與該-R
選項一起使用。從rsync(1)
手冊頁:
-R, --relative
Use relative paths. This means that the full path names speci‐
fied on the command line are sent to the server rather than just
the last parts of the filenames. This is particularly useful
when you want to send several different directories at the same
time. For example, if you used this command:
rsync -av /foo/bar/baz.c remote:/tmp/
... this would create a file named baz.c in /tmp/ on the remote
machine. If instead you used
rsync -avR /foo/bar/baz.c remote:/tmp/
then a file named /tmp/foo/bar/baz.c would be created on the
remote machine, preserving its full path. These extra path ele‐
ments are called "implied directories" (i.e. the "foo" and the
"foo/bar" directories in the above example).