
Muitas, muitas vezes por dia, desejo copiar um ou dois arquivos de ~/src/some/path/or/other
e ~/src/some/different/path
para minha máquina local, para /srv/www/some/path/or/other
e /srv/www/some/different/path
, respectivamente, em um host de teste. Os dois caminhos são idênticos depois de ~/src e /srv/www respectivamente - embora obviamente eu normalmente omita o nome do arquivo no segundo argumento para scp, que uso atualmente.
O problema é que muitas vezes eu digito a invocação do scp inteiramente e copio o arquivo para o diretório errado ou sobrescrevo um arquivo diferente porque tab complete consegue me enganar. Eu poderia sincronizar novamente toda a minha árvore local com o servidor por meio de ssh, mas às vezes não quero fazer upload de tudo ainda - apenas um arquivo específico.
Acho que o que quero é uma ferramenta CLI simples que me permita fazer (talvez com um pouco de configuração) o equivalente moral de:
jkg5150@dev-laptop:~/src/myproject$ funkytool path/to/file another/path/another-file mydevhost.mycompany.com
...e file
copiei para mydevhost.mycompany.com:/srv/www/myproject/path/to/
e another-file
copiei para mydevhost.mycompany.com:/srv/www/myproject/another/path/
.
Certamente há um truque que estou perdendo - ou devo apenas escrever um?
Responder1
Use rsync com a -R
opção. Na rsync(1)
página de manual:
-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).