Friday, April 19, 2013

One-liner to copy text to a remote host

Occasionally, I want to copy a short line of text to a remote computer. For instance, I have an URL for some real cool web site which, for whatever reason, I want to send to a remote host. I can always put the text in a file, and transfer it via scp.

$ cat > coolurl.txt

$ scp coolurl.txt peter@

Or, you can use the following one-liner command:

$ echo 'http://really-cool-web-site/'|ssh peter@ 'cat >coolurl.txt'

The one-liner uses only simple commands such as echo, ssh and cat. It saves you the step of creating a new file on the local machine.

The text is saved to a file called coolurl.txt on the remote computer under your home directory.

Let me know your favourite way to accomplish the same thing.

1 comment:

Anonymous said...

I have this file on the remote host as ~/bin/log

#!/bin/bash -
if [ "$1" ]; then
grep "$1" $LOGFILE
echo -n 'make entry '
case $REPLY in
'') cat $LOGFILE;;
*) echo $(date '+%F %T') $REPLY >> $LOGFILE;;
exit 0

Then, on each host I sit at, I have
alias log='ssh remote_host_address "bin/log"'

So if I don't enter any text, I get the file's contents
and if I do enter text it is time stamped and saved.
If I type log "searchterm" I get all the lines containing searchterm

P.S. Too bad code can't be offset to retain indentation