keepcalculator

06 Numbers and Powers

Compare Files on Mac: The Built-In Ways

Card No. 06·4590 · Updated · Standard formula, shown below

To compare two files on a Mac, the quickest method is using the built-in diff command in Terminal, which shows line-by-line differences. For a more visual, side-by-side comparison without installing anything, you can use the FileMerge application that comes with Apple’s Command Line Tools. This article explains both methods, plus how to compare folders and what to do when files are too large or binary.

Why Compare Files on a Mac?

Comparing files is a daily task for developers, writers, and anyone managing multiple versions of a document, script, or configuration file. You might need to see what changed between two versions of a report, verify that a backup copied correctly, or check whether two folders contain the same files. The Mac offers several ways to do this, ranging from a simple text command to a full graphical diff tool, and the right choice depends on your comfort with the Terminal and the type of files you’re comparing.

The Terminal’s diff Command

The diff command has been part of Unix-like systems for decades and is included with macOS. It compares two text files line by line and prints the differences to the Terminal. To use it, open Terminal (in /Applications/Utilities) and type:

diff /path/to/file1.txt /path/to/file2.txt

If the files are identical, you’ll see no output. If they differ, you’ll see lines prefixed with < (from the first file) and > (from the second file). For example:

< The quick brown fox
> The quick red fox

This tells you that line 1 in the first file says “brown” and in the second file says “red.” For a more readable output, use diff -u (unified format) which shows a few lines of context around each change, similar to how version control systems display differences.

If you prefer a side-by-side view, try diff -y which prints two columns. To ignore whitespace and case differences, add -w and -i respectively. The diff command is powerful, but it can be overwhelming for large files because it prints the entire content of changed lines. For large files, you might want to pipe the output to less to scroll through it: diff file1 file2 | less.

FileMerge: The Graphical Diff Tool

Apple provides a free graphical comparison tool called FileMerge, but it isn’t installed by default. It comes with the “Command Line Tools” package, which you can install by typing xcode-select --install in Terminal and following the prompts. Once installed, FileMerge lives in /Applications/Xcode.app/Contents/Applications/ (if you have Xcode) or /Library/Developer/CommandLineTools/usr/bin/ (if you only installed the tools). You can launch it by typing opendiff in Terminal, or by navigating to the folder in Finder.

FileMerge shows two files side by side, with differences highlighted in color. It also allows you to merge changes from one file into the other, which is useful when you want to combine edits from two people. To use it, open FileMerge, choose “Compare” from the File menu, and select the two files. The interface shows a vertical bar with colored blocks representing differences; clicking a block jumps to that difference. You can then use the buttons to copy changes left or right.

Comparing Folders and Multiple Files

Sometimes you need to compare entire directories, not just individual files. The diff command can do this recursively with the -r flag:

diff -r /path/to/folder1 /path/to/folder2

This will list all files that differ, and for each differing text file, show the actual differences. It also reports files that exist in one folder but not the other. For a cleaner summary, you can use diff -rq which only lists which files differ, without showing the content changes. This is handy for verifying that a backup is identical to the original.

If you want to compare more than two files, the built-in tools are limited. diff compares exactly two files. For three-way comparisons (common in code reviews), you would need a third-party tool. However, for most everyday tasks, comparing two files at a time is sufficient.

When Files Are Binary or Too Large

The diff command works on text files. If you try to compare binary files (like images, PDFs, or compiled programs), it will output “Binary files differ” and stop. To see a hex dump of the differences, you can use cmp -l which prints the byte positions and values that differ. For example:

cmp -l file1.jpg file2.jpg

This outputs lines like 123 101 102 meaning at byte 123, the first file has octal value 101 and the second has 102. This is not very human-friendly, but it can help confirm that two binary files are different and where.

For very large text files (hundreds of megabytes), diff can be slow and memory-intensive. In such cases, you might want to compare file checksums instead. The shasum or md5 commands compute a hash of a file. If two files have the same hash, they are almost certainly identical. This is much faster than a line-by-line diff, but it doesn’t tell you what changed, only that they are different. For example:

shasum -a 256 file1.txt file2.txt

How to Choose the Right Method

Here’s a quick guide to picking the right tool for your situation:

  • Quick text comparison – Use diff in Terminal. It’s fast and always available.
  • Visual side-by-side – Use FileMerge. It’s easier to read and allows merging.
  • Compare folders – Use diff -r or diff -rq.
  • Binary files – Use cmp -l or compare checksums.
  • Large files – Use checksums (shasum) instead of diff.

If you need to compare files regularly, you might also consider installing a dedicated app from the App Store, but the built-in options cover most needs without extra cost.

Common Mistakes and Tips

A frequent mistake is using diff on files that have different line endings (Windows vs. Mac). Files saved on Windows often have carriage return + line feed (CRLF), while Mac and Linux use just line feed (LF). This can cause diff to report every line as different. To ignore line ending differences, use the --strip-trailing-cr flag with diff.

Another tip: if you want to see differences in a more human-friendly way, you can use the sdiff command (which is like diff -y but with interactive merging) or use the git diff command if you have Git installed. git diff --no-index file1 file2 works even outside a Git repository and produces a colored, unified diff that many find easier to read.

Remember that diff returns an exit code of 0 if files are identical, 1 if they differ, and 2 if there’s an error. You can use this in scripts to automate comparisons.

Frequently Asked Questions

Can I compare files without Terminal?

Yes, you can use FileMerge, which is a graphical app, but you still need to install Command Line Tools first. Once installed, you can launch FileMerge from Spotlight (press Cmd+Space and type “FileMerge”) and use its interface to select files. There is no built-in Finder feature to compare files, so a third-party app or Terminal is required for a direct comparison.

Does macOS have a built-in diff tool like WinMerge?

macOS includes diff and FileMerge (with Command Line Tools), but it does not have a standalone graphical diff app like WinMerge installed by default. FileMerge serves a similar purpose, though it lacks some advanced features. For a WinMerge-like experience, you may need to download a third-party app, but the built-in tools are sufficient for most users.

How do I compare two files in Finder?

Finder doesn’t have a built-in compare feature. You can select two files and use the Quick Actions, but there’s no “Compare” option. To compare, you’ll need to use Terminal or FileMerge. A workaround is to open both files in TextEdit and use the “Edit > Find > Find and Replace” with a custom script, but that’s impractical. The easiest way is to use the Terminal command diff as described above.

What does the output of diff mean?

The output of diff uses < for lines from the first file and > for lines from the second file. A line like 1c1 means “change line 1 in the first file to line 1 in the second file.” Other markers: a (append), d (delete). In unified format (-u), lines starting with - are from the first file, + from the second, and spaces indicate context lines.

Can I compare files in different formats, like PDF or Word?

No, the built-in tools compare plain text files. For PDF or Word documents, you would need specialized software like Microsoft Word’s “Compare Documents” feature (for .docx) or Adobe Acrobat’s file comparison (for PDFs). These tools are not built into macOS, so you’d need to have those applications installed.

Is there a way to compare files and ignore whitespace?

Yes, the diff command has flags to ignore whitespace: -w ignores all whitespace differences, -b ignores changes in the amount of whitespace, and -B ignores blank lines. For example, diff -w file1 file2 will ignore spaces and tabs, which is useful when comparing code where indentation might differ.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →