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
http://really-cool-web-site/

$ scp coolurl.txt peter@192.168.1.112:

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

$ echo 'http://really-cool-web-site/'|ssh peter@192.168.1.112 '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 -
LOGFILE="~/repo/log"
if [ "$1" ]; then
grep "$1" $LOGFILE
else
echo -n 'make entry '
read
case $REPLY in
'') cat $LOGFILE;;
*) echo $(date '+%F %T') $REPLY >> $LOGFILE;;
esac
fi
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