LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Compare folder/files from remote server (https://www.linuxquestions.org/questions/linux-newbie-8/compare-folder-files-from-remote-server-605143/)

shipon_97 12-07-2007 03:07 AM

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 .. ...

matthewg42 12-07-2007 03:37 AM

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.

shipon_97 12-07-2007 03:53 AM

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 .. ...

matthewg42 12-07-2007 04:14 AM

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?)

colucix 12-07-2007 04:20 AM

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.

shipon_97 12-07-2007 04:43 AM

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.

colucix 12-07-2007 05:34 AM

Quote:

Originally Posted by shipon_97 (Post 2983051)
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.

matthewg42 12-07-2007 05:41 AM

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/

lela2416 01-22-2010 07:09 AM

Quote:

Originally Posted by colucix (Post 2983093)
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?

White Tea Citrus 01-24-2010 04:56 PM

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

colucix 01-24-2010 05:58 PM

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.

lela2416 01-26-2010 05:55 AM

Quote:

Originally Posted by colucix (Post 3839102)
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!

colucix 01-26-2010 12:04 PM

Quote:

Originally Posted by lela2416 (Post 3840817)
Thanks a lot, I liked this solution and it worked!

You're welcome! :)


All times are GMT -5. The time now is 02:53 PM.