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
rsyncpost.
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 targetHere’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 protected]:/var/www/staging.somelongdomainname.com/public_html/Where:
-iidentity_fileSelects the file from which the identity (private key) for public key authentication is read. This option is directly passed to ssh(1).
-PportSpecifies the port to connect to on the remote host. Note that this option is written with a capital 'P', because
-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.
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 bobThat would equal to the following ssh command:
$ ssh \
-p 5555 \
-i ~/.ssh/id_rsa \
[email protected]And now the shortname can be used, the Host, to form an ssh connection:
$ ssh helloSetup 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-completionThen 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.