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

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](/sync-large-quatities-of-files-or-directories-between-local-machine-and-server-with-rsync/).

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):

```shell
$ 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"](/how-to-setup-and-manage-multiple-ssh-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:

```shell
$ 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:

```shell
$ 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](/opening-the-matryoshka-pointless-package-management-hell)):

```shell
$ brew install bash-completion
```

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

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

> ℹ️ I think if you use
> [fish as your shell](/how-to-install-and-use-fish-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:

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

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

```shell
$ 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`:

```shell
$ 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](/sync-large-quatities-of-files-or-directories-between-local-machine-and-server-with-rsync/)
for moving files around.
