Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-23-2017, 08:01 AM   #1
Registered: Aug 2012
Posts: 789

Rep: Reputation: Disabled
Send output of command to a different server

How can I perform the following in one command? If not one, what about two?

# Currently logged on server 999.999.999.999
/path/to/command 1234 > /path/to/file
scp /path/to/file username@
rm /path/to/file
Same question, but if logged on to 999.999.999.999 instead of
Old 02-23-2017, 08:04 AM   #2
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
Try something like
command 1234 | ssh user@remote_host "cat >> file_name"
Old 02-23-2017, 08:16 AM   #3
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Thanks nodir,

So, run command 1234,pipe it to sshing into remote host, and cat'ing the results to file_name? Why >> instead of >?

Also,what if I was currently logged on remote_host?
Old 02-23-2017, 08:21 AM   #4
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
i copied the command from a script where i want to append to a textfile.
Hence the >> instead of a >
(in short: a typo. sorry :-).

And yes, i guess you gave the general summary of it (the quotes around " cat > filename" do matter, if i recall correct).
ssh command
only executes the command remote, after that you are still local (in case that was a question)

Not sure about the other question, user is logged in remote, i guess it doesn't matter.

good luck.

Last edited by nodir; 02-23-2017 at 08:23 AM.
Old 02-23-2017, 09:20 AM   #5
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
> as apposed to >>

> writes to the file if something is already there is gets written over will the new stuff.
>> appends whatever is being sent to what is already there.

using >> on first write will still get a file written, just the next time you will not have to change the > to >> if you are keeping a log type file, it saves recoding.

Using nodir example with a little change to path to directory first.
command 1234 | ssh user@remote_host "cat >> /path/to/dir/file_name"
but as his name suggest he has no directory so maybe that is why he did not included a path to a directory in his example. (just playing off of your user name nodir. Nothing personal)

Last edited by BW-userx; 02-23-2017 at 09:23 AM.
Old 02-23-2017, 09:45 AM   #6
Senior Member
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
that would work, the question I have is, do you have root access to these machines? If so could you use an NFS share? or fuse SSHFS to mount the remote directory locally and then just read/write the file/directory as if it were local? Just a thought.
Old 02-23-2017, 10:02 AM   #7
Senior Member
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
rsync would be better, it already has the ability to handle this with the --remove-source-files flag

thinking something like

/path/to/command 1234 > /path/to/file && rsync -avz --remove-source-files /path/to/file user@host:/destination/path

Last edited by r3sistance; 02-23-2017 at 10:15 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to send the output of send command to a file auma78 Linux - Newbie 2 01-23-2011 12:06 AM
Ned to send command output to email Thaidog Programming 2 11-21-2009 11:58 AM
How to send command output to file?! mr_aliagha Linux - General 2 02-27-2009 01:06 PM
Make a program can collect linux command output regularly and send to a tftp server fragon Programming 2 04-07-2008 04:49 PM
IS it possible to send command output to a file as well as the screen helptonewbie Programming 6 09-16-2007 01:13 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:09 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration