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 12-07-2007, 04:07 AM   #1
shipon_97
Member
 
Registered: Oct 2005
Location: Bangladesh
Posts: 490

Rep: Reputation: 30
Compare folder/files from remote server


Friends ,


Suppose I have two same folders in machine A (Linux) and in machine B (Linux) . Now I am compare these two files using "diff" command .
I mean running diff command in following way :

diff folder1 (machine A) folder2 (machine B)

Can I do it ?

Plz help .. ...
 
Old 12-07-2007, 04:37 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
You said file and folder. Which do you mean?

Diff compares the contents of two files. It does not (directly) compare directories.

Diff compares files only - not URLs. When you say remote, what do you mean? If the remote files are mounted on the local system using NFS or SMB or something else which allows you to see them in the local filesystem this is fine, but if you access the files using HTTP or FTP, this is a little more complicated.
 
Old 12-07-2007, 04:53 AM   #3
shipon_97
Member
 
Registered: Oct 2005
Location: Bangladesh
Posts: 490

Original Poster
Rep: Reputation: 30
diff command in remotely

Thx matthewg42 .

Acyually I have a file called "file1" in Linux A .
In Linux B i have same file caled 'file2'

Now i need to run diff command in a shell script .
But I cannot go to the file2 in LinixB easily . So that I need ur help how can I comapre this two files where files are in different machines ......

Waiting ur kind reply .. ...
 
Old 12-07-2007, 05:14 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Some more questions:
  • Do you have the ability to ssh from A to B?
  • What sort of results are you looking for - do you want to know just if the files are different, or do you want to know what the differences are?
  • If you want to know what the differences are, is normal diff output what you want?
  • Are the files big and the network slow (i.e. is it practical to copy the file from B to A?)
 
Old 12-07-2007, 05:20 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can compare files remotely using ssh like this
Code:
ssh <remote-host> "cat /path/to/remotefile" | diff - /path/to/localfile
where <remote-host> is the address of the remote machine. This will execute "cat /path/to/remotefile" on the remote host and will compare it with /path/to/localfile. The command
Code:
diff - /path/to/localfile
means compare standard input with the content of /path/to/localfile. The standard input is passed through the pipe.
Maybe you have to elaborate to suite to your needs. For example, you may want to generate private/public ssh keys to avoid prompting for password. Also if you want to compare files with blank space in their name you have to escape it, e.g.
Code:
ssh <remote-host> "cat remote\ file" | diff - local\ file
I hope this will help.
 
Old 12-07-2007, 05:43 AM   #6
shipon_97
Member
 
Registered: Oct 2005
Location: Bangladesh
Posts: 490

Original Poster
Rep: Reputation: 30
diff in remoter server

sorry colucix ,

ssh is not directly connected to the remote server without password , is it ?
It shows follwoign error :

[root@server ~]# ssh 192.168.101.129 "cat /tmp/file2" | diff - /root/file1
diff: cannot compare `-' to a directory
connect to address 192.168.101.129: Connection refused
Trying krb4 ssh...
connect to address 192.168.101.129: Connection refused
trying normal ssh (/usr/bin/ssh)
sshd: 0826-826 The host name for your address is not known.
 
Old 12-07-2007, 06:34 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by shipon_97 View Post
ssh is not directly connected to the remote server without password , is it ?
No, of course. You have to setup public key authentication for passwordless connection. See man ssh and man ssh-keygen for details.
Quote:
[root@server ~]# ssh 192.168.101.129 "cat /tmp/file2" | diff - /root/file1
diff: cannot compare `-' to a directory
connect to address 192.168.101.129: Connection refused
Trying krb4 ssh...
connect to address 192.168.101.129: Connection refused
trying normal ssh (/usr/bin/ssh)
sshd: 0826-826 The host name for your address is not known.
This means two things: first, /root/file1 on the local machine is a directory, not a file, isn't it? So it would be more tricky to compare directories by this method. Second, it looks like the remote machine does not resolve the host name of your local machine and refuse connection. Try to investigate this problem before proceeding.
 
Old 12-07-2007, 06:41 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Here's how to configure ssh to work using public key authentication (which you can use to do remote commands without being prompted for a password each time):

http://sial.org/howto/openssh/publickey-auth/
 
Old 01-22-2010, 08:09 AM   #9
lela2416
LQ Newbie
 
Registered: Jan 2010
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
No, of course. You have to setup public key authentication for passwordless connection. See man ssh and man ssh-keygen for details.

This means two things: first, /root/file1 on the local machine is a directory, not a file, isn't it? So it would be more tricky to compare directories by this method. Second, it looks like the remote machine does not resolve the host name of your local machine and refuse connection. Try to investigate this problem before proceeding.
Hi all, I know this thread is quite old, but I am facing a similar issue now...what I need to know is "it is possible to combine cat , diff and ssh to compare the content of two directories ( and so recursively of all the files into them ) located on two different machines?
 
Old 01-24-2010, 05:56 PM   #10
White Tea Citrus
LQ Newbie
 
Registered: Sep 2009
Location: Slovakia
Distribution: Trisquel 5.0 Dagda
Posts: 27

Rep: Reputation: 0
I only can imagine the following:
ssh your@connection find ./folder -type f -exec cat \;>> ./freespace; find ./folder -type f -exec cat \; >> ./evenmorespace; diff ./freespace ./evenmorespace

Last edited by White Tea Citrus; 01-24-2010 at 05:59 PM.
 
Old 01-24-2010, 06:58 PM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Or use sshfs to mount the remote directory through ssh (it requires FUSE) and then just diff recursively. Here is a quick and dirty example (suppose you have to compare /path/to/local/dir and /path/to/remote/dir)
Code:
$ mkdir local-mount-point
$ sshfs user@host:/path/to/remote/dir local-mount-point
$ diff -r /path/to/local/dir local-mount-point
<omitted results of comparison>
$ fusermount -u local-mount-point    # to unmount
The only glitch is that on many systems the fusermount command has the setuid attribute set and you can run it only by gaining root privileges. A common workaround is to add yourself (or ask the system administrator to add you) to the fuse group.
 
1 members found this post helpful.
Old 01-26-2010, 06:55 AM   #12
lela2416
LQ Newbie
 
Registered: Jan 2010
Posts: 2

Rep: Reputation: 0
Smile

Quote:
Originally Posted by colucix View Post
Or use sshfs to mount the remote directory through ssh (it requires FUSE) and then just diff recursively. Here is a quick and dirty example (suppose you have to compare /path/to/local/dir and /path/to/remote/dir)
Code:
$ mkdir local-mount-point
$ sshfs user@host:/path/to/remote/dir local-mount-point
$ diff -r /path/to/local/dir local-mount-point
<omitted results of comparison>
$ fusermount -u local-mount-point    # to unmount
The only glitch is that on many systems the fusermount command has the setuid attribute set and you can run it only by gaining root privileges. A common workaround is to add yourself (or ask the system administrator to add you) to the fuse group.
Thanks a lot, I liked this solution and it worked!
 
Old 01-26-2010, 01:04 PM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by lela2416 View Post
Thanks a lot, I liked this solution and it worked!
You're welcome!
 
  


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
compare folder contents babysnake Linux - Newbie 4 02-09-2007 08:04 AM
shell script to compare filese b/w local and remote and delete files from local serve dsids Linux - Networking 9 08-23-2006 08:20 AM
Share a remote folder through linux server newuser455 Linux - General 2 10-01-2004 10:37 PM
copy files from a server to another..both remote Santosh_d Linux - Networking 1 03-09-2004 08:52 AM
Remote Mounting? (Mount remote folder on local filesystem) mac_phil Linux - Networking 1 11-15-2003 04:48 PM


All times are GMT -5. The time now is 12:28 AM.

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