Copy files from local to remote, and tab complete in the remote server
I've been reaching for Cyberduck for years when I've had to move a file to a server. But, scp bash command let's you do all that right in the prompt.
rsyncpost also :)
Copying files from local to server, or vice versa, happens best with the
scp (secure copy) command.
rsync is also a great command, but this post looks into
$ scp options source target
$ scp ~/dev/test.html -p 5555 email@example.com:/var/www/staging.somelongdomainname.technology/public_html/
The problems in this command might be:
- It’s laborious to type the port, username, host name:
-p 5555 firstname.lastname@example.org. You might have a default port, but still it’s a lot of typing.
- It’s even more work to type the path in the remote server. The change of typos might be quite high.
Turns out that remote tab completion is possible, and ssh credentials can be stored into a config file and then referred with a short alias.
Let’s set those up next.
Authenticate to the server
For this all to be possible, ssh authentication to the server is needed. I have a post on that: “How to create ssh keys and manage multiple keys”. Have a look if authentication is black magic for you.
Basically, the credentials are added to
Host hello HostName 123.456.789.01 Port 5555 IdentityFile ~/.ssh/id_rsa User bob
Then you can just do:
$ ssh hello
And you’re securely in, no passwords asked. Next, the tab completion.
Setup remote host tab completion
If on OS X, a program called
bash-completion is needed, install with homebrew (if homebrew is not familirriar, check my article on it):
$ brew install bash-completion
Then add the following snippet to your
if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion fi
Next, let’s put this all to use.
We set out host alias to “hello” above, if a files is wanted to be uploaded to server, we can simply go like:
$ scp test.html hello:/var/www/staging.somelongdomainname.technology/public_html/
To get a file from the remote server, just flip it and reverse it:
$ scp hello:/var/www/staging.somelongdomainname.technology/public_html/ test.html
And yes, tab completion is possible on remote paths :) It’s not instant though, because it has to go look for the path over the internet, but so much better than nothing. It doesn’t get any easier than that!
Moving a directory
Use the recursive flag
$ scp -r project/ hello:/var/www/domain.com/public_html/
This has been a pain point for me a long time, I never thought that remote tab completion would be possible, live and learn. Also, rsync is very viable option for moving files around.
Also checkout my
rsync post, it brushes the subject.