27

I have two directories - one from earlier backup and second from newest backup. How do i compare what changes were made to files in directory from newest backup on Linux? Also how do i display changes in for example text and php files - i'm thinking about something like revision history on wikipedia where you see old version on one side of the screen and newest version on other and changes are highlighted. How do i achieve something like that?

edit: How do i also compare remote dir with local?

Phil
  • 1,839
  • 6
  • 27
  • 33

6 Answers6

41

From diff man page:

If both from-file and to-file are directories, diff compares corresponding files in both directories, in alphabetical order; this comparison is not recursive unless the -r or --recursive option is given. diff never compares the actual contents of a directory as if it were a file. The file that is fully specified may not be standard input, because standard input is nameless and the notion of ‘‘file with the same name’’ does not apply.

So to compare directories: diff --brief -r dir1 dir2

To compare files side by side: diff --side-by-side file1 file2

Sean Staats
  • 750
  • 6
  • 6
  • very cool, i'll give it a try – Phil Aug 26 '09 at 16:30
  • ...i wonder how do i compare remote directory to local one? – Phil Aug 26 '09 at 16:43
  • 1
    You would have to mount the remote directory to the local machine. However, you can remotely compare files like so: ssh REMOTE_SERVER "cat /path/to/some/file" | diff --side-by-side /path-to-some-file - One other thing, the 'sdiff' command works like 'diff --side-by-side' if you want to save some typing. – Sean Staats Aug 26 '09 at 18:13
  • it's high time to introduce version control on your files see svn, git (,maybe cvs). – asdmin Aug 26 '09 at 23:09
  • how do i mount it, then? – Phil Aug 27 '09 at 12:18
  • (you have upvote from me both on answer and comment, btw) – Phil Aug 27 '09 at 12:18
  • 2
    Use NFS to mount the remote directory. On the remote server(server-b), edit /etc/exports and put the following in it: /path/of/directory/to/share server-a.ip.address(ro,no_root_squash) Start NFS on server-b (/etc/init.d/nfs start) Mount to your local server(server-a) by adding the following to /etc/fstab: server-b.ip.add:/path/of/directory/to/share /mnt/server-b nfs rsize=32768,wsize=32768,rw 0 0 Then on server-a, mkdir /mnt/server-b; mount /mnt/server-b Thanks for the upvotes. – Sean Staats Aug 27 '09 at 12:40
  • 2
    You can use `sshfs` to create an ad-hoc temporary networked directory, instead of using NFS. – Dan Andreatta Apr 07 '10 at 21:47
  • To also compare symbolic links as links, add the `--no-dereference` option – Jonas Berlin Dec 24 '21 at 07:18
3

Assuming:

  • we are on www1, comparing with remote www2
  • there is configured public ssh key authentication from local www1 to remote www2
  • we compare as the same user on local www1 and remote www2
find /var/www/html/ -name "*" -exec md5sum -b {} \; | grep -v "/var/www/html/exclude_this dir" > local.md5
ssh www2 "find /var/www/html/ -name '*' -exec md5sum -b {} \; | grep -v /var/www/html/exclude_this dir > remote.md5"
scp www2:remote.md5 .
diff local.md5 remote.md5 
user69366
  • 149
  • 3
3

You really want to combine the power of rsync to reduce bandwidth consumption with the power of diff to give you flexible, well um diffs.

So something like this:

cp -R $local $bak
rsync $server:$remdir/* $local/
rsync $local/ $server:$remdir/* 
diff -wur $local $bak

I guess you could tweak this a bit if you were doing it often use rsync instead of cp in the first line - obviously in the last line you have the full power of diff to format it however you like. Probably with y in the OPs case

Downside of this approach is you end up using twice as much local space, but at less than $1/gig who cares?

ErichBSchulz
  • 151
  • 3
1

Do diff old_dir new_dir > diff.txt for side by side differences on the same server.

For remote files:

For example : ABC is existing server and XYZ is your remote server and directory name is 123.

Step 1: Rename existing directory 123 on ABC Server as 123_ABC.

ABC:/Home > mv 123 123_ABC

Step 2: Create a new directory on the server ABC:

ABC: > mkdir 123_XYZ

Step 3: Copy all files from directory 123 on XYZ server to 123_XYZ directory on ABC server:

XYZ/123 > scp * userid@ABC: /123_XYZ

This will copy all files from the directory on your XYZ server to your ABC server/ 123_XYZ directory.

Step:4 : Do the diff between both directories:

Now go to ABC server and perform diff between 123_ABC and 123_XYZ

ABC > diff 123_ABC 123_XYZ > diff.txt

The command above will save the diff results in to diff.txt in the same path.

You may compare the differences then.

Thank you,

Mehul

asciiphil
  • 3,036
  • 3
  • 26
  • 52
mehul
  • 11
  • 1
0

Or you can use two files with the out put of list of files. And then compare those two files. For example:

/path/to/compare/remote$: ls > remote-files 

/path/to/compare/local$: ls > local-files 

Download one of the files.

-rw-r--r-- 1 1015 1015    26624 2005-06-14 13:10 FILE.TXT  

to FILE.TXT

Use diff (diff -y remote-files local-files > diff-files) to compare them side by side. Open the diff-files and check it. Each line with > means a different file.

Mark Henderson
  • 68,316
  • 31
  • 175
  • 255
0

AIDE Advanced Intrusion Detection Environment (AIDE) is a file integrity checker for UNIX operating systems. Its purpose to provide reporting on the integrity of data on supported file systems. By running AIDE multiple times on the target host you can determine what files are changing. By running AIDE multiple times on different hosts you can determine what files and permissions are different. Then use a gui diff tool on the reported "different" files.

Or use a gui diff tool like meld, guiffy, kdiff3, diff, vimdiff, gvimdiff, Emacs, Kompare, Diffuse, Easydiff, TkDiff or xxdiff. Most will do directory diffs in addition to file diffs. You'll need to mount the remote drive using NFS, SMBFS or SSHFS as others have mentioned.