To compare two files on a Mac from the command line, use the built-in diff command followed by the paths to the files, like diff file1.txt file2.txt. It outputs the lines that differ, showing which lines are unique to each file, and returns no output if the files are identical.
What diff Actually Shows You
The diff command is part of the Unix toolset that ships with macOS. It compares two files line by line and prints the differences in a specific format. When you run diff old.txt new.txt, each difference is shown with a line like 2,3c2,4 that tells you which line numbers in the first file correspond to which lines in the second, followed by the actual lines. A < prefix marks lines from the first file, and a > prefix marks lines from the second file. For example, if old.txt contains “apple” and new.txt contains “orange”, diff will output:
1c1
< apple
---
> orange
That means line 1 in the first file changed to line 1 in the second. If a line was added or deleted, you’ll see a (added) or d (deleted) instead of c (changed).
Common diff Options You’ll Actually Use
Plain diff output can be hard to read, so macOS includes several options that make it more useful. The most frequently used are:
-u– Unified format, which shows a few lines of context around each change. This is the same format used by Git and most code editors.-c– Context format, similar to unified but with a slightly different layout.-i– Ignore case differences (treats “Apple” and “apple” as equal).-w– Ignore all whitespace differences (spaces, tabs, but not line breaks).-B– Ignore blank line changes.-r– Compare directories recursively, not just files.-q– Quiet mode; only report whether files differ, not how.
For most day-to-day use, diff -u file1 file2 gives the clearest picture. If you want to see only a summary, diff -q file1 file2 prints “Files file1 and file2 differ” or nothing if they’re identical.
How to Read the Unified Format
Unified format is what you’ll see from Git and many other tools, so it’s worth understanding. A typical output looks like this:
--- file1.txt 2025-01-01 12:00:00
+++ file2.txt 2025-01-01 12:05:00
@@ -1,3 +1,3 @@
apple
-orange
+banana
grape
The --- line is the first file, +++ is the second. The @@ line tells you the line numbers: -1,3 means lines 1–3 in the first file, +1,3 means lines 1–3 in the second. Lines prefixed with a space are unchanged context. A - prefix means the line was removed from the first file, and a + prefix means it was added in the second. So in the example, “orange” was replaced by “banana”.
Comparing Directories and Using diff with Other Commands
You can also compare entire directories. Running diff -r dir1 dir2 will compare all files inside both directories recursively, reporting which files differ and what the differences are. This is handy for checking if two project folders are in sync. If you only want a list of files that differ, add -q to suppress the detailed output.
Another common pattern is to pipe diff into other commands. For example, diff file1 file2 | less lets you scroll through a long diff page by page. You can also save the output to a file with diff file1 file2 > changes.patch and later apply it with patch.
Alternatives: colordiff, git diff, and GUI Tools
Plain diff output is monochrome, which can be hard to scan. A common improvement is colordiff, a wrapper that adds color to the output. You can install it with Homebrew (brew install colordiff) and then run colordiff file1 file2. If you’re working in a Git repository, git diff -- file1 file2 shows differences with color and a slightly different format, but it only works on tracked files.
For a graphical comparison, macOS includes FileMerge (part of the Xcode developer tools) and you can open it from the command line with opendiff file1 file2. Third-party tools like Kaleidoscope, Beyond Compare, and Meld offer side-by-side views, but they are separate downloads. The built-in diff is always available and is the fastest option for quick checks.
Common Problems and How to Fix Them
If diff returns nothing, the files are identical. If you get No such file or directory, check that the paths are correct and that you’ve escaped spaces (e.g., diff My\ File.txt Other\ File.txt). If the output shows every line as different, the files likely have different line endings (CRLF vs LF) or encoding (UTF-8 vs UTF-16). You can normalize line endings with dos2unix or use diff --strip-trailing-cr to ignore carriage returns. For encoding, convert files first with iconv.
Another common issue is that diff treats files as binary if they contain null bytes, and prints “Binary files differ” instead of a line-by-line comparison. Use diff -a to force text mode, though the output may be messy for truly binary data.
Frequently Asked Questions
How do I ignore whitespace differences in diff?
Use the -w flag to ignore all whitespace, or -b to ignore changes in the amount of whitespace (but still treat a space vs. no space as different). For example, diff -w file1 file2 will not report differences caused only by extra spaces or tabs.
How do I see only the lines that differ, not the whole file?
By default, diff only prints the differing lines and a small header. If you want even less, use diff -q to just report whether files differ. To see only the added or removed lines without context, filter the output with grep: diff file1 file2 | grep '^[<>]'.
Can diff compare two files with different names?
Yes, diff doesn’t care about file names. It compares the contents of whatever two paths you give it. The names appear only in the header of the output.
What does “No newline at end of file” mean?
It means one file ends without a final newline character, while the other does. This is a common difference and is reported by diff in unified format. It’s not an error, just a note that the files differ in that final character.
How do I compare files in different folders?
Just provide full or relative paths: diff /path/to/file1 /path/to/file2. For comparing entire folders, use diff -r folder1 folder2.
Is there a way to make diff output more readable?
Yes, use -u for unified format with context, or install colordiff for color highlighting. You can also pipe to less -R to view colored output page by page.
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
- Gadgets After Support Ends: Do They Improve?
- Delete Google Search Results About You
- OnePlus Buds Pro 3 Price Drop: Are They Worth It?
- Cloud Migration Ways to Prepare
- Remote Work Productivity Hacks That Actually Work
- Epic Games Removes Dark and Darker? Fact Check