Since I work on remote systems all the time, I use SCP repeatedly to transfer files around. One of the more cumbersome tasks is specifying the remote file or directory location.
So I wrote a helper script to make it easier. It’s called scptarget, and it generates targets for SCP, either the source or the destination.
For instance, if I want to copy a file down from a remote server, I run scptarget like this and copy the output:
Then it’s easy to paste it into my SCP command on my local system:
I usually use remotecopy (specifically
remotecopy -c) to copy it so that I don’t even have to touch my mouse.
Here are a few example uses.
First, without any arguments, it targets the current working directory. This is useful when I want to upload something from my local system to where I’m remotely editing files.
Specifying a file targets the file directly.
Absolute paths are handled correctly:
Vim SCP targets
Vim supports editing files over SCP, so passing
-v in generates a target that it can use:
And to edit, just pass that in to Vim:
IP based targets
Sometimes I need the target to use the IP of the server instead of its hostname. This usually happens with development VMs (a la Vagrant), which are only addressable via IP. Passing
-i to scptarget causes it behave this way. Under the hood, it uses getip, which is a script I wrote that prints out the first IP of the current host. If there is no non-private IP, then it will return the first private IP. (I am fully aware that there may be better ways of doing the above. Let me know if you have a better script.)
That’s it. I find it incredibly useful and I hope you do too.