ローカルパスの一部を複製するよりスマートな SCP

ローカルパスの一部を複製するよりスマートな SCP

一日に何度も、ローカル マシンから~/src/some/path/or/otherとローカル マシン上の 1 つまたは 2 つのファイルを、テスト ホストのとにそれぞれコピーしたいことがあります。2 つのパスはそれぞれ ~/src と /srv/www 以降は同じです。ただし、現在使用している scp の 2 番目の引数ではファイル名を省略するのが普通です。~/src/some/different/path/srv/www/some/path/or/other/srv/www/some/different/path

問題は、私がよく scp 呼び出しを間違えて間違ったディレクトリにファイルをコピーしたり、タブ補完がうまく機能して別のファイルを上書きしたりすることです。ローカル ツリー全体を 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

オプション付きで rsync を使用します-Rrsync(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).

関連情報