I often find myself needing to download files to my local box via SCP. Which means entering the hostname, the path, and the filename in to my terminal window. However, I’m really lazy, if I can’t auto complete it or cut and paste it, I’m not happy. Enter this simple Bash function:

1
to_scp() { echo scp "$USER@`hostname -f`:$PWD/$@"; }

and this tcsh alias:

1
alias to_scp 'echo scp $user@`hostname -f`:$cwd/\!^'

In either shell, typing:

1
to_scp filename

outputs something like scp user@net.example.com:/var/www/filename, which you can then cut and paste into you local window sticking a . or a directory on the end:

1
scp user@net.example.com:/var/www/filename .

Added bonus: if you leave the filename off, you get just the directory, scp user@net.example.com:/var/www/, handy for uploading:

1
scp filename user@net.example.com:/var/www/

Of course, if you followed my previous tip about Remote SSH Usernames you can further reduce the aliases to:

1
to_scp() { echo scp "`hostname -f`:$PWD/$@"; }

and:

1
alias to_scp 'echo scp `hostname -f`:$cwd/\!^'

Laziness for the win!

Comments