clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

How to use secure copy to copy files from local machine to a remote, and tab complete the remote server directories

Filed under: Server— Tagged with: bash, ssh, rsync

The classic method is to use Cyberduck (or other sftp client) for moving a files to a server. But the scp bash command let’s you do all that right in the prompt.

👉 If moving a ton of files, you might want to read my rsync post.

Copying files from your local machine to a remote server, or vice versa happens best with the scp (secure copy) command. rsync is also a great, but this post looks into scp.

Basic scp syntax:

scp options source target

Here’s an example where the directory path on the remote machine is really long (split into multiple lines for readability):

$ scp
  -i ~/.ssh/id_rsa \
  -P 5555 \
  ~/dev/test.html \
  bob@123.4567.890:/var/www/staging.somelongdomainname.com/public_html/

Where:

-i identity_file
Selects the file from which the identity (private key) for public key authentication is read. This option is directly passed to ssh(1).
-P port
Specifies the port to connect to on the remote host. Note that this option is written with a capital 'P', because -p is already reserved for preserving the times and modes of the file.

If you find yourself typing a command like that a lot, you might want to alias the login details and setup remote-host tab-completion.

Remote tab completion can be done over ssh, and the ssh credentials can be stored into a config file, then referred with a short alias.

Let’s set those up next.

Configure shorthand SSH credentials

For this all to be possible, ssh authentication to the server is needed.

💁‍♂️ I have a post SSH: "How to create ssh keys and manage multiple keys". Take a look if authentication is black magic to you.

An SSH host can be configured in a ~/.ssh/config file. Like so:

Host           hello
HostName       123.456.789.01
Port           5555
IdentityFile   ~/.ssh/id_rsa
User           bob

That would equal to the following ssh command:

$ ssh \
  -p 5555 \
  -i ~/.ssh/id_rsa \
  bob@123.456.789.01

And now the shortname can be used, the Host, to form an ssh connection:

$ ssh hello

Setup remote host tab completion

If OS X, a program called bash-completion is needed, install with homebrew (if homebrew is not familiar, check this article):

$ brew install bash-completion

Then add the following snippet to your ~/.bash_profile:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
  . $(brew --prefix)/etc/bash_completion
fi

ℹ️ I think if you use fish as your shell, this should just work.

Next, let’s put this all to use.

Using the scp command to move files

The host alias is set to "hello" (above), and a file can be uploaded like so:

$ scp test.html hello:/var/www/staging.somelongdomainname.com/public_html/

To download a file from the remote server to the current working directory:

$ scp hello:/var/www/staging.somelongdomainname.com/public_html/test.html .

Tab completion is possible on that long and tedious remote path. It’s not instant though, because it has to go look for the path over the internet. But it doesn’t get much easier.

Moving a directory with scp

Use the recursive flag -r:

$ scp -r project/ hello:/var/www/domain.com/public_html/

Conclusions

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.

Comments would go here, but the commenting system isn’t ready yet, sorry.

  • © 2022 Antti Hiljá
  • About
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • I love u!