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
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
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 \ email@example.com:/var/www/staging.somelongdomainname.com/public_html/
-pis 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.
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 \ firstname.lastname@example.org
And now the shortname can be used, the Host, to form an ssh connection:
$ ssh hello
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
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.
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.
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.
Comments would go here, but the commenting system isn’t ready yet, sorry. Tweet me @hiljaa if you want to make a correction etc.