Diff Compare Files Linux: How to Use diff
To compare two files in Linux, use the diff command. It shows the differences line by line, so you can quickly see what changed between two versions of a file. This guide explains the command’s syntax, common options, and how to interpret its output, plus practical examples you can try immediately.
What diff Does and Why It Matters
The diff utility compares two files line by line and reports the differences. It is a standard tool on virtually every Linux and Unix system, and it is the foundation for version control systems like Git and for generating patches. Instead of manually scanning two files to spot changes, diff gives you a precise, machine-readable list of what differs.
When you run diff file1 file2, it exits with a status code: 0 means no differences, 1 means differences were found, and 2 means an error occurred (like a missing file). This makes it easy to use in scripts and automated checks.
Basic Syntax and First Examples
The simplest form is:
diff file1 file2
Suppose you have two files, old.txt and new.txt. Running diff old.txt new.txt might produce output like:
2c2 < This is the old line. --- > This is the new line.
Here’s how to read that:
2c2means line 2 in the first file was changed to line 2 in the second file.<lines come from the first file.>lines come from the second file.---separates the two file’s content.
Other common markers are d (deleted) and a (added). For example, 3d2 means line 3 in the first file was deleted to match the second file (which has only 2 lines).
Making Output More Readable: Unified and Side-by-Side
The default output is compact but can be hard to scan. Two options improve readability:
Unified Format (-u)
The unified format groups changes with a few lines of context, making it easier to see what changed around each difference. It is the format used by Git and most code review tools.
diff -u old.txt new.txt
Output looks like:
--- old.txt 2025-01-01 10:00:00 +++ new.txt 2025-01-01 10:05:00 @@ -1,3 +1,3 @@ This is a common line. -This is the old line. +This is the new line. This is another common line.
The @@ header tells you the line numbers in each file. Lines starting with - are from the first file, + from the second, and a space means the line is unchanged.
Side-by-Side Format (-y)
To see two columns, use diff -y file1 file2. It displays both files next to each other, with markers like | for changed lines, < for lines only in the first, and > for lines only in the second. You can adjust the column width with -W 100 (100 characters).
Ignoring Whitespace and Case
Often you only care about actual content changes, not spacing or capitalization. Use these options:
-wignores all whitespace (spaces and tabs).-bignores changes in the amount of whitespace (e.g., multiple spaces vs. one).-iignores case differences.-Bignores changes that only add or delete blank lines.
For example, diff -w -i old.txt new.txt will treat “Hello World� and “hello world� as identical, as long as the only difference is whitespace and case.
Comparing Directories Recursively
To compare all files in two directories, add the -r (recursive) option:
diff -r dir1 dir2
This walks through both directories and reports which files differ, which are only in one directory, and which are identical (with -q it only lists files that differ, not the details). For a quick summary, use diff -rq dir1 dir2 – it prints only the names of files that differ or are unique to one directory.
Common Problems and How to Avoid Them
- Binary files:
difftreats binary files as different unless you use--text(or-a) to force text comparison, which may produce garbage. For binary comparison, usecmpormd5suminstead. - Missing files: If one file doesn’t exist,
diffprints an error and exits with status 2. Check file paths first. - Line endings: Files from Windows use CRLF (
\r\n) while Linux uses LF (\n). This makes every line appear different. Strip the carriage returns withdos2unixor usediff --strip-trailing-cr. - Large files: For very large files,
diffcan be slow. Consider usingcmpfor a quick binary check, orgit diff --no-indexfor a more efficient comparison.
Using diff Output to Create Patches
One of the most powerful uses of diff is generating a patch file that can be applied to another copy of the file. To create a patch:
diff -u old.txt new.txt > changes.patch
To apply the patch to another file (say, old.txt on a different machine), use the patch command:
patch < changes.patch
This is how many open-source projects distribute code changes before Git became ubiquitous. Always use the -u option for patches, as it includes context that makes the patch more robust.
Alternatives to diff
While diff is the classic tool, other utilities offer different strengths:
- cmp – compares byte by byte and reports the first difference. Useful for binary files.
- comm – compares sorted files line by line and shows lines unique to each file or common to both.
- git diff –no-index – if you have Git installed, this gives a colorized, word-level diff with a familiar interface.
- vimdiff – opens a visual side-by-side diff in the Vim editor, allowing interactive merging.
- meld or kdiff3 – graphical diff/merge tools for desktop environments.
For most command-line tasks, diff with -u is the best balance of simplicity and readability.
Frequently Asked Questions
How do I see only the lines that differ, not the context?
Use diff --unchanged-line-format= --old-line-format='< %L' --new-line-format='> %L' file1 file2 to suppress unchanged lines. Alternatively, use grep on the output to filter lines starting with < or >.
How do I compare two files ignoring trailing spaces?
Use diff -b file1 file2 to ignore changes in the amount of whitespace, or diff -w to ignore all whitespace. For trailing spaces specifically, -b is sufficient.
What is the difference between diff and cmp?
diff compares line by line and shows all differences, while cmp compares byte by byte and stops at the first difference. cmp is faster for binary files; diff is better for text.
Can diff compare files in different directories?
Yes, just provide full paths: diff /path/to/file1 /path/to/file2. For directories, use diff -r dir1 dir2.
How do I get a list of files that differ between two directories?
Use diff -rq dir1 dir2. The -q (brief) option makes it print only the names of files that differ, not the details.
Why does diff show every line as different?
This usually happens when files have different line endings (CRLF vs LF). Convert the files with dos2unix or use diff --strip-trailing-cr to ignore the carriage return.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.