Compare two files to see if theyre different

sometimes you might be working on a file with a colleague and be sent a version with the same filename as one you already have. But how do you know if that version has been updated? You can check the filesize but that's not 100% reliable—your colleague may have added data, but also removed an equal amount.

There are two simple methods for quick file comparison at the command-line. The first is to use the md5sum command, which outputs a unique 32-digit number based on the contents of the file. You'd then compare the md5sum output for each file side-by-side (one tip is that i usually compare a few digits from the start and a few from the end—if these are the same then it's extremely likely the rest will be too). To use the command, just type md5sum filenamel, and then md5sum filename2.

md5sum falls down a little on larger files, because it can take a while to generate the checksum. Another trick is to use the diff command. Just type diff filenamel filename2. If there's no difference, there will be no output. if there is a difference, you'll see one of two things: the message that "binary files filename1 and filename2 differ", which is likely if you're comparing, say, Word documents. Alternatively, the screen will fill with text, showing the difference between the files on a line-by-line basis. This is only likely to happen if diff thinks that the file is plain text (or, indeed, if the file actually is plain text, in which case you could redirect the output into a file for viewing later: diff filenamel filename 2 > changes).

Was this article helpful?

0 0

Post a comment