How to use diff to compare text files
Learn how to compare two files with diff, read its output, choose a format, and ignore whitespace, case, blank-line and line-ending differences safely.
The diff command compares two text files line by line and reports what would need to change to turn the first file into the second. It ships with most Linux and macOS systems and is available on Windows through environments such as WSL or Git Bash. This guide covers the basic comparison, how to read the output, and how to make diff ignore formatting differences such as whitespace, letter case, blank lines and line endings so that only meaningful changes remain.
What diff actually compares
diff does not modify either file. It reads both, treats each line as a unit, and prints the smallest set of line insertions, deletions and changes it can find. The comparison is based on the stored characters, not on how the text looks on screen. Two lines that appear identical can still differ if one contains a tab and the other spaces, if one ends with a Windows-style carriage return, or if one uses a lookalike Unicode character. By default, diff reports all of these as differences. Ignore options change what counts as a difference during the comparison; they never rewrite the files.
Running a basic comparison
Suppose you have two short files. The examples below are illustrative: they show the expected format of GNU diff output for these inputs rather than captured results from a specific system.
# a.txt
apple
banana
cherry
# b.txt
apple
blueberry
cherry
$ diff a.txt b.txt
2c2
< banana
---
> blueberry
In this default (normal) format, the line 2c2 means line 2 of the first file was changed into line 2 of the second. Lines starting with < come from the first file, lines starting with > come from the second, and --- separates them. Other change markers are a for added lines and d for deleted lines. If the files are identical, diff prints nothing.
diff also returns an exit status, which matters in scripts: 0 means no differences were found, 1 means differences were found, and a value greater than 1 means an error occurred, such as a missing file.
Choosing an output format
The default format is compact but hard to scan. Most people prefer the unified format, which shows changes in context.
$ diff -u a.txt b.txt
--- a.txt <timestamp>
+++ b.txt <timestamp>
@@ -1,3 +1,3 @@
apple
-banana
+blueberry
cherry
The @@ line says the hunk covers three lines starting at line 1 in each file. Lines beginning with - were removed, lines beginning with + were added, and lines beginning with a space are unchanged context. The timestamps are filled in from the files' modification times.
| Option | Output style | Best for |
| (none) | Normal format with <, > and line-number commands | Minimal output, older tooling |
| -u | Unified format with context lines | Reading changes, sharing patches, code review |
| -c | Context format with *** and --- sections | Tools or reviewers that expect the older context style |
| -y | Side-by-side columns | Visually comparing short files in a wide terminal |
| -q | Only reports whether the files differ | Quick checks and scripts |
| -s | Also reports when files are identical | Confirming a match explicitly |
| -r | Recursive comparison of directories | Comparing whole folders of text files |
Ignoring formatting differences
When files come from different editors, operating systems or formatters, real content changes can be buried under noise. GNU diff provides options that relax the comparison. These are not all part of the POSIX standard, which guarantees only a small set of options (including -b, -c, -u and -r), so the BSD-derived diff on some systems may support fewer of them or behave slightly differently. Check man diff or diff --help on your system before relying on one.
| Option (GNU long form) | What it ignores | Watch out for |
| -i (--ignore-case) | Uppercase versus lowercase letters | Does not fold styled Unicode letters into plain ones |
| -b (--ignore-space-change) | Changes in the amount of whitespace, and trailing whitespace | Still detects whitespace added where there was none, such as "a b" versus "ab" |
| -w (--ignore-all-space) | All whitespace within lines | Treats "a b" and "ab" as equal, which can hide real changes |
| -Z (--ignore-trailing-space) | Whitespace at the end of lines only | Narrowest whitespace option; safest when only trailing spaces differ |
| -E (--ignore-tab-expansion) | Differences caused by tabs versus equivalent spaces | Depends on tab-stop assumptions |
| -B (--ignore-blank-lines) | Changes consisting only of blank lines | A blank line inside a hunk that also has real changes is still shown |
| --strip-trailing-cr | Carriage returns at line ends (Windows CRLF versus Unix LF) | Only affects the character at the end of each line |
| -I REGEX (--ignore-matching-lines) | Changed lines that all match the pattern | Ignores a hunk only if every changed line in it matches |
Options can be combined. A common choice for comparing the same document saved on Windows and Linux, with inconsistent indentation, is shown below.
diff -u --strip-trailing-cr -b -B old.txt new.txt
Start with the narrowest option that removes the noise you know about, then widen only if needed. -Z or --strip-trailing-cr changes very little about what counts as a difference; -w and -i change a lot. Over-broad ignores can make files look equivalent when a change in spacing or case actually matters, such as in code, configuration files, passwords or case-sensitive identifiers.
Finding differences you cannot see
If diff reports a change but the lines look the same, the difference is usually in invisible or lookalike characters. Common causes include:
- Trailing spaces or tabs at the end of a line
- Tabs in one file where the other has spaces
- Windows line endings (carriage return plus line feed) in one file only
- A missing newline at the end of the last line, which diff reports as "No newline at end of file"
- Non-breaking spaces or other Unicode space characters pasted from web pages or word processors
- Lookalike characters, such as letters from another alphabet or styled Unicode letters produced by "fancy text" or "font generator" tools
- The same accented letter stored in composed and decomposed Unicode forms
- Different text encodings, such as UTF-8 versus UTF-16 or a byte order mark at the start of one file
To reveal tabs and line endings, GNU cat can mark them: -A shows tabs as ^I, carriage returns as ^M and line ends as $. In bash, you can feed that view straight into diff:
diff <(cat -A a.txt) <(cat -A b.txt)
Unicode-level differences need more care. diff compares bytes, so it does not normalize Unicode forms, and -i only folds ordinary case pairs. Text that has been restyled with Unicode substitutes (for example mathematical bold letters standing in for plain ones) or reversed character by character will show up as entirely different lines, even if it reads the same to a person. Purely visual effects such as CSS mirroring or right-to-left display do not change the stored characters, so diff will not see them at all. Whether -w treats non-ASCII spaces like a non-breaking space as whitespace depends on the implementation and locale, so test with your own files rather than assuming. If you need to compare such text meaningfully, normalize both files first with a tool you trust, then diff the normalized copies and keep the originals unchanged.
Which approach suits your comparison
diff is not the only way to compare text, and the right choice depends on what kind of difference you care about.
| Approach | Unit of comparison | Strengths | Tradeoffs |
| diff (default or -u) | Lines | Available almost everywhere, scriptable, produces patch-ready output | A one-word change marks the whole line as changed |
| diff with ignore options | Lines, with relaxed matching | Filters out whitespace, case, blank-line and line-ending noise | Broad options can hide meaningful changes; option support varies |
| cmp | Bytes | Pinpoints the first differing byte; works on binary files | No readable summary of what changed |
| git diff --no-index | Lines, or words with --word-diff | Works on files outside a repository; word-level and colored output | Requires Git to be installed |
| Graphical or online diff viewers | Lines and often characters | Easier visual review of long documents | Online tools send your text to a third party; features vary by tool |
For prose, where a single edited word should not flag a whole paragraph, a word-level view such as git diff --no-index --word-diff is often easier to read. For configuration, code and data files, line-based diff -u is the usual standard because its output can be applied with patch or reviewed in pull requests.
Keeping the output readable and usable
- Color: GNU diff and git diff can color output, but color may be emitted as escape codes when output is redirected or pasted. Use --color=never (or omit color) when saving or sharing results.
- Accessibility: Do not rely on red and green alone. The -, +, < and > markers carry the same meaning in plain text and work with screen readers and color-blind viewing.
- Copy and paste: Pasting diff output into chat apps, word processors or email can convert tabs to spaces, wrap long lines or strip leading spaces, which breaks patches. Share patches as attached files or inside code blocks, and test that the destination preserves whitespace.
- Patches: Output from diff -u can be applied with patch, but only if whitespace and line endings survive transfer intact.
- Scripts: Check the exit status rather than parsing text. Status 1 means "different," not "failed," so scripts using set -e may stop unexpectedly unless they handle it.
- Sensitive content: Local diff keeps your data on your machine. Before pasting contracts, credentials, logs or personal data into an online comparison site, consider whether that content should leave your system, and redact it if not.
Limits of this guide
The option names and output formats described here follow GNU diff conventions; the sample outputs are illustrative, not captured from a tested system. Behavior on BSD or macOS diff, handling of non-ASCII whitespace under -w, and how specific editors, terminals or online tools render diff output were not verified for this guide. Confirm option support with your system's manual page and run a quick test on sample files before relying on a comparison for important work.