Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can copy a file to a remote Linux machine with no problem with

scp file user@host: /pathtowrite_file

However, I'm having difficulty writing to a file from one linux machine to another. Here is what I attempted:

echo 'Some Text' > /remotefile.txt | ssh user@remotehost

The notice that I get is

stdin: is not a tty

At any rate, the file on the remote machine does not reflect the text sent 'Some Text'.

share|improve this question
up vote 32 down vote accepted

You can use the "cat" command to create the remote file.

echo 'Some Text' | ssh user@remotehost "cat > /remotefile.txt"
share|improve this answer
    
but, I want to write to the existing file on the remote machine. – suffa Mar 14 '12 at 17:36
1  
the "cat > /remotefile.txt" runs on the remote machine and if you want to append to a existing file you replace ">" by ">>" – Aragorn Mar 14 '12 at 17:42
    
@suffa: Are you trying to append to an existing file? Or do you want to overwrite the file but fail if it doesn't already exist? Or what? – David Schwartz Mar 14 '12 at 17:52
    
@ David Schwartz - either. I would like to overwrite, and fail if it doesn't exit ... but, I would take just appending at this point. – suffa Mar 14 '12 at 19:06
    
` echo 'Some Text'|ssh user@remotehost "tee -a /remotefile.txt"` worked for me as well. – Simply Seth Oct 13 '14 at 20:20

It's also possible to use dd to append to a file. Maybe a bit obscure but useful if output redirection on the remote host isn't possible.

cat ~/.ssh/id_rsa.pub | ssh user@remote.host 'dd of=.ssh/authorized_keys oflag=append conv=notrunc'

This example appends your public key to the authorized_keys file on the remote host.

(Source: http://www.rsync.net/resources/howto/ssh_keys.html)

share|improve this answer

Create a script as below:

# !/bin/bash

read -p "Enter target server IP : " server
echo "Enter root password for $server : " ; read -s password

yum install sshpass -y

sshpass -p "$password" ssh -o strictHostKeyChecking=no root@$server echo "your text goes here" >> /remotefile.txt
share|improve this answer

A bit shorter than the other answer:

ssh user@remotehost "echo Some Text > /remotefile.txt"

share|improve this answer
    
True; this will work — if writing a single line of text to the remote file is really all the OP wants to do.  The command in the question looks like a proof-of-concept test.  The question says, "writing to a file from one Linux machine to another."  If the user wants to run an arbitrary command (or sequence of commands) — and not just an echo — locally, then your answer doesn't help, and the accepted answer is the way to do it. – Scott Nov 17 '15 at 21:44
    
@Scott Multiple commands are also possible using this approach. Like most things Linux, TMTOWTDI: ssh localhost "echo 'hi'; echo 'hello'; echo 'well, hello there!' – g33kz0r Nov 18 '15 at 1:01
    
Cool dude, thanks for the downvote. I guess that's easier than actually addressing my point? PS your comment above makes no sense. – g33kz0r Nov 18 '15 at 19:36
    
OK, you want more criticism?  (1) Why in the world are you saying ssh localhost?  That's just muddying the water, not making it clearer.  (2) By not showing redirection in your modified example, you raise the question of whether you know how to group commands and send all the output to one file.  … (Cont’d) – Scott Nov 18 '15 at 23:44
    
(Cont’d) …  (3) You’ve missed my point, by focusing on the phrase “a single line of text”.  My point was that the OP might want to do something more sophisticated that writing known text to the remote file — he might want to run a local command and send the output to the remote file, as in command (localhost) > file (remotehost).  If you can adapt your answer to address that general case, please do so. – Scott Nov 18 '15 at 23:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.