每天,我都会多次想要将一两个文件从本地机器~/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 将整个本地树 rsync 到服务器,但有时我暂时不想上传整个文件——只是上传一个特定的文件。
我想我想要的是简单的 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
使用带有-R
选项的 rsync。来自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).