33

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?

0

7 Answers 7

48

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

9
  • very cool, i'll give it a try
    – Phil
    Aug 26, 2009 at 16:30
  • ...i wonder how do i compare remote directory to local one?
    – Phil
    Aug 26, 2009 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. Aug 26, 2009 at 18:13
  • 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. Aug 27, 2009 at 12:40
  • 2
    You can use sshfs to create an ad-hoc temporary networked directory, instead of using NFS. Apr 7, 2010 at 21:47
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?

2
  • 1
    cp --reflink=auto --sparse=always to use no space for the copy depending on underlying FS.
    – Tom Hale
    Aug 2, 2019 at 5:28
  • Things are not cheap all over the world.
    – Rodrigo
    Mar 19, 2021 at 22:29
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 
1
  • this actually works but 1. you don't need to scp 2. directory paths need to be the same 3. will pollute both directories with extra files 4. file order is not guaranteed to be the same serverfault.com/a/1128839/349476 Apr 17, 2023 at 10:29
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

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.

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.

0

Based on user69366's answer but with a little tweaking:

Assuming you're currently in the local directory

find . -type f | sort | xargs -I {} md5sum -b {} > /tmp/local.md5 > /tmp/local.md5
ssh USER@SERVER 'cd REMOTE_PATH && find . -type f | sort | xargs -I {} md5sum -b {} > /tmp/local.md5' > /tmp/remote.md5
diff /tmp/local.md5 /tmp/remote.md5

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .