Terminal 101: Using Versions in the Command Line
Posted 04/23/2012 at 12:19pm
| by Cory Bohon
Document comparison has been around for a while. You write up a document draft, make your edits, and then compare those edits with the original document. This function is usually reserved for developers, but end-users can utilize it, too. Apple included this function by launching Versions in Lion, which is the same basic concept but is not supported by all applications. So, this week we’ll show you how to use diff, a file comparison utility that’s available on almost every iteration of OS X.
Comparing Apples to Apples
Remember that when you compare files using the diff command, you’ll want to compare similar file types. For instance, you cannot compare a .txt file with an .rtf file.
Using diff to compare files

If you have two text files and wish to compare them, you’ll type the following command into the terminal:
diff first_file second_file
Replace first_file with the path and filename to the first file you wish to compare (if its in the same directory you’re browsing, then you’ll just need to type the filename and extension); replace second_file with the path and filename of the second file that you wish to compare with the first.

The diff command is prescriptive. This means that it will tell you changes (line-by-line) that need to be made to the first file in order to make it look like the second file. A greater-than sign (>) denotes lines that are in the second file, but not the first; and, a less-than sign (<) denotes lines that are in the first file, but not the second.
By reading these lines of output, you can see the differences between the two files.
Using diff to compare folders
You can also use diff to compare the contents of two folders to see which files are the same between the two directories. To do this, type the following command into the terminal:
diff first_dir second_dir
Replace first_dir with the name of the first directory you wish to compare; replace second_dir with the name of the second directory to compare.

You’ll get similar output to comparing a file, but you’ll be given a list of the files that are in the first folder, but not in the second.
Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.