LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 06-14-2013, 04:28 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 187

Rep: Reputation: 1
Add one file's content into another remotely


If I want to add one file's contents into another, I use cat command.
Quote:
$ cat file1 >> file2
and it adds contents of file1 into file2
but the problem is file2 is in diferent system.
Other system's IP is 192.168.1.10
Now I want to add file1 content into file2 in single command line.
I do not want to do following process.
Quote:
1. copy the file on remote server.
2. log into remote server using ssh
3. add content using cat command.
I want to do it using single command line. I have tried following commands and could not get success.
Code:
$ cat /root/file1 >> 192.168.1.10:/root/file2
$ echo `cat /root/file1` >> 192.168.1.10:/root/file2
$ scp `cat /root/file1` >> 192.168.1.10:/root/file2
Even I have tries to do it something like this which I did not want but it couldn't work for me too.
Quote:
$ scp /root/file1 192.168.1.10:/root/file3 | ssh 192.168.1.10 cat /root/file3 >> 192.168.1.10:/root/file2
Please help me. I will be thankful.
 
Old 06-14-2013, 04:40 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You could try this:
Code:
$ scp /root/file1 192.168.1.10:/root/file3 && ssh 192.168.1.10 "cat /root/file3 >> /root/file2"
This copies the file to the remote machine (the scp part) and if this succeeds (the && part) the ssh command will add the contents of file 3 to file2 (remotely).
 
1 members found this post helpful.
Old 06-14-2013, 06:07 AM   #3
mddnix
Member
 
Registered: Mar 2013
Location: Bengaluru, India
Distribution: Redhat, Arch, Ubuntu
Posts: 498

Rep: Reputation: 137Reputation: 137
Code:
cat file1 | ssh root@192.168.1.10 "cat >> /root/file2"
 
1 members found this post helpful.
Old 06-15-2013, 01:33 AM   #4
unclesamcrazy
Member
 
Registered: May 2013
Posts: 187

Original Poster
Rep: Reputation: 1
thanks mddesai for your reply but it is not working on my system

thanks druuna... it worked... as you could see in my reply i almost reached there but there is no 90 or 95% in shelll... there is either 100 or 0 and i was 0.

but i didnt want to use two commands... can it happen in a way like
echo `cat.......`
something like this.
or
something using sed or awk but in one command.

thanks
 
Old 06-15-2013, 03:55 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by unclesamcrazy View Post
thanks mddesai for your reply but it is not working on my system
mddesai's solution does work on my side. Too bad it doesn't work for you. Not sure why this is so.

Quote:
thanks druuna... it worked...

but i didnt want to use two commands... can it happen in a way like
echo `cat.......`
something like this.
or
something using sed or awk but in one command.
There might be other solutions, but I doubt if any of those will use less then 2 commands.
 
Old 06-16-2013, 08:37 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
As with drunna, mddesai's soln works for me; are you sure you did it correctly? What error did you get?
 
Old 06-16-2013, 08:47 PM   #7
WildPossum
Member
 
Registered: Feb 2004
Location: Sydney - Australia
Distribution: OpenSUSE, Ubuntu, Mythbuntu, iMedia, Embedded Linux
Posts: 44

Rep: Reputation: 18
Your scp / sftp should work but I think you will find that root access is denied as a standard scp / sftp configuration standard.
Try using your user account to do the same as above for the scp commands you produced; and I thinkk you should be OK.
 
Old 06-16-2013, 09:52 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,599

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by mddesai View Post
Code:
cat file1 | ssh root@192.168.1.10 "cat >> /root/file2"
A slightly shorter version:

Code:
ssh root@192.168.1.10 "cat >> /root/file2" <file1
 
Old 06-17-2013, 01:47 AM   #9
unclesamcrazy
Member
 
Registered: May 2013
Posts: 187

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by chrism01 View Post
As with drunna, mddesai's soln works for me; are you sure you did it correctly? What error did you get?
When I try mddesai's solution, I get
Code:
Host key verification failed.
lost connection
But I do not get this error if I try druuna's solution.
Same error I get when I try jpollard's solution.

Only druuna's solution worked for me.

By the way, thanks to all of you.
 
Old 06-17-2013, 02:54 AM   #10
bloodstreetboy
Member
 
Registered: May 2012
Posts: 198
Blog Entries: 3

Rep: Reputation: 37
Quote:
Originally Posted by unclesamcrazy View Post
I do not get this error if I try druuna's solution.
Same error I get when I try jpollard's solution.

Only druuna's solution worked for me.
No it is not possible, if you get the error for other two solutions, you should get it for your working solution too.
It is nothing different. All three should be worked fine or all three should not be worked.

It can't be possible if only one is working and other two not.
 
Old 06-17-2013, 03:05 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by unclesamcrazy View Post
When I try mddesai's solution, I get
Code:
Host key verification failed.
lost connection
But I do not get this error if I try druuna's solution.
Same error I get when I try jpollard's solution.
Can you post the exact command you are using that generates this error.
 
Old 06-17-2013, 04:31 AM   #12
unclesamcrazy
Member
 
Registered: May 2013
Posts: 187

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by bloodstreetboy View Post
It can't be possible if only one is working and other two not.
Believe me I am not lying
Click image for larger version

Name:	Screenshot from 2013-06-17 13:55:17.png
Views:	23
Size:	36.9 KB
ID:	12732
Sorry, I do not have any other proof.
Quote:
Originally Posted by druuna View Post
Can you post the exact command you are using that generates this error.
As I did not want to share my original command but if I do not share I can not prove myself true.
Here are the commands :
Code:
# cat /root/.ssh/id_rsa.pub | ssh 192.168.1.10 "cat >> /root/.ssh/authorized_keys"
# ssh 192.168.1.10 "cat >> /root/.ssh/authorized_keys" </root/.ssh/id_rsa.pub
 
Old 06-17-2013, 04:56 AM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,599

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
That error has nothing to do with the command, but is because the key you have in the known_hosts file doesn't match the one presented by the host.
 
  


Reply


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
Dividing content of one file by content of another larspend Linux - Newbie 5 04-12-2011 09:00 PM
remotely new user add with password in samba server anindyabhattacharjee Linux - Enterprise 2 01-01-2008 04:48 AM
add content to .config file wahaha Linux - Wireless Networking 1 08-27-2006 11:14 AM
Add file content to a variable (bash)? LinuxSeeker Programming 4 12-19-2005 02:41 PM
Add users remotely, daemon? The Jesus Linux - Networking 1 08-14-2003 11:56 AM


All times are GMT -5. The time now is 06:00 PM.

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