2

I want to compare the content of two disk images. (For example two disk images with gutsy n edgy) Is there a straight forward command for it in Unix?

3 Answers3

5

To compare disk images byte-for-byte and list all the differing bytes, you can run

cmp -l /path/to/first.img /path/to/second.img

This will compare everything, including any unoccupied space. If these are filesystem images and you want to do a file-against-file comparison, you need to mount them, then use a file comparison tool such as diff or rsync.

1
rsync --delete -n -aPc /disk1/ /disk2/

This will not copy any files to disk2 nor delete and files from disk1 thanks to the -n, but it will show you any files which differ between the two filesystems.

Borealid
  • 240
  • 1
  • 3
  • 1
    The question isn't clear but you may have to mount that image before you can compare (if it is an iso) – trent Mar 03 '11 at 06:52
  • But i suppose for this to work my disk images would have to be mounted. Without mounting and simply having it as X.img files can i make a comparison? How about creating a hash value and comparing? –  Mar 06 '11 at 16:13
  • Yes, you could do `md5sum foo.iso bar.iso` – Steven Mar 06 '11 at 16:47
  • @Steven: Hashes are vulnerable to hash collision. You could just `diff img1.img img2.img` if you want to be sure the images are identical. One disadvantage is that this won't report *how* the images are different... – Borealid Mar 06 '11 at 20:04
  • If you're going to worry about md5 collisions on iso files, you should worry about collisions with rsync (which uses md5 along with a checksum). – Steven Mar 06 '11 at 22:22
  • @Steven: You can set it to compare file contents. But what I was really saying is that there's no need to compare hashes when you want to compare files; comparing the hashes takes longer, in fact, because `md5sum` can't bail out when it finds the first difference, whereas `diff` can. – Borealid Mar 06 '11 at 22:28
  • rsync compares contents of files by breaking them into chunks and hashing them. diff would be faster if he only wants to know they diff. `cmp` is best if he actually wants to compare the files. – Steven Mar 07 '11 at 00:49
  • Could I copy the diskimage.raw changes to /dev/sda disk? – Milor123 Dec 30 '16 at 14:40
0

Here's a command that will compare /dev/sda1 to /dev/sdb1, with a GUI to show progress; partitions tend to be big inputs so it's nice to know how long you need to wait for completion. The command is for a particular 6 terabyte disk, the progress data will only be as good as your size estimate.

gprog --size-estimate 6001173463040 < /dev/sda1 | cmp - /dev/sdb1

gprog is available from http://stromberg.dnsalias.org/~strombrg/gprog/ I'm the author.

You could conceivably do something similar with pv, though I'm not sure what it'd do with any output from cmp.

user1084684
  • 155
  • 1
  • 7