File comparison tools for Linux: top 5 compared
File comparison tools for Linux let you see exactly what changed between two files or directories, whether you are reviewing code, checking configs, or verifying a backup. This article compares the five most commonly used tools—diff, cmp, meld, diff-so-fancy, and rsync—and explains how to choose the right one for your task.
Why you need a file comparison tool
When you edit a configuration file, update a script, or receive a revised document, you often need to know precisely what changed. A visual diff shows those changes line by line, saving you from reading two files side by side. For directories, comparison tools reveal which files were added, removed, or modified, which is essential for syncing backups or deploying code.
Linux has a rich ecosystem of comparison utilities, from simple command-line tools that output text to graphical applications with side-by-side panes. The right choice depends on whether you are comparing two files, two directories, or a whole tree of files, and whether you prefer a terminal or a GUI.
The classic command-line tools: diff and cmp
The diff command has been part of Unix since the 1970s and remains the most widely used file comparison tool. It compares two files line by line and outputs the differences in a text format. For example:
diff file1.txt file2.txt
This prints lines that are unique to each file, prefixed with < (file1) and > (file2). To get a unified format (the one used by Git and most patch tools), use -u:
diff -u file1.txt file2.txt
The cmp command is more basic: it compares byte by byte and reports the first difference. It is useful when you only need to know whether two binary files are identical, or when you want the exact byte offset of the first change.
Both tools are installed by default on virtually every Linux distribution, so they are the go-to choice when you cannot install extra software.
Graphical diff tools: meld and others
If you prefer a visual interface, meld is the most popular GUI diff tool on Linux. It shows two or three files side by side, with changed lines highlighted in color. You can edit files directly in the comparison view, which makes it ideal for merging changes manually. Meld also compares directories, showing which files differ and letting you copy changes between sides.
Other GUI options include kdiff3 (part of the KDE suite), kompare, and diffmerge. These tools are especially useful when you are reviewing a large number of changes, such as after a code merge or a package upgrade that modified several config files.
To install meld on Debian/Ubuntu:
sudo apt install meld
On Fedora:
sudo dnf install meld
Comparing directories and entire trees
When you need to compare two directories, diff can do it with the -r (recursive) flag:
diff -r dir1 dir2
This lists all files that differ, and for text files it shows the actual line changes. For a more concise summary, use -q (brief) to only list which files differ:
diff -rq dir1 dir2
For a more powerful directory sync tool, rsync is not strictly a comparison tool, but its --dry-run option shows exactly what would change when syncing two directories:
rsync -av --dry-run dir1/ dir2/
This lists every file that would be copied, updated, or deleted. It is the standard way to verify a backup or a deployment before actually making changes.
Making diff output prettier: diff-so-fancy and colordiff
Raw diff output is functional but not easy to scan. diff-so-fancy is a popular wrapper that reformats diff output with clearer headers, highlighted changes, and better alignment. It is often used with Git to make code reviews more readable. To use it, install the script and pipe diff output through it:
diff -u file1.txt file2.txt | diff-so-fancy
colordiff is a simpler tool that adds ANSI color to diff output, making it easier to spot changes in a terminal. Both are available in most distribution repositories.
How to choose the right tool
The table below summarizes the main tools and their best use cases:
| Tool | Best for | Interface |
|---|---|---|
| diff | Quick line-by-line comparison of text files | Terminal |
| cmp | Checking if two files are identical (binary or text) | Terminal |
| meld | Visual comparison and merging of files/directories | GUI |
| diff-so-fancy | Readable, colored diff output in the terminal | Terminal |
| rsync –dry-run | Previewing what a directory sync would change | Terminal |
For most users, diff is the first tool to learn. If you work with code, integrate it with Git using git diff, which uses the same underlying algorithm. For a graphical review, meld is the most intuitive. For binary files, cmp is the simplest way to confirm they are identical.
Common mistakes and misconceptions
One common mistake is using diff on binary files—it will output “Binary files differ” but nothing more useful. Use cmp for binaries, or sha256sum to compare checksums instead.
Another misconception is that diff shows all differences in a file. By default, it only shows the changed hunks, not the entire file. To see the full context, use -c (context) or -u (unified) with a larger context size, e.g., diff -u5 shows 5 lines of context around each change.
When comparing directories, forgetting the -r flag will cause diff to treat directories as files and fail. Always use -r for recursive comparison.
Finally, diff exit code is 0 if files are identical, 1 if they differ, and 2 if there is an error. This is useful in scripts, but many users are surprised that a difference is not an error.
Frequently Asked Questions
What is the difference between diff and cmp?
diff compares text files line by line and shows the changes in a human-readable format. cmp compares files byte by byte and only reports the first difference. Use diff for text, cmp for binaries or when you only need to know if files are identical.
Can I compare files with different line endings (CRLF vs LF)?
Yes. diff has a --strip-trailing-cr option that ignores carriage return characters, making files with Windows and Unix line endings compare cleanly. Alternatively, convert one file with dos2unix first.
How do I compare files in two different directories?
Use diff -r dir1 dir2 to recursively compare all files. For a summary of which files differ without showing the changes, add -q. For a preview of a sync, use rsync -av --dry-run dir1/ dir2/.
Is there a tool to merge changes from two files into one?
Yes. meld and kdiff3 allow you to manually merge changes from two or three files. For automatic merging, diff3 can merge three files, but it requires a common ancestor file and may produce conflicts that you must resolve manually.
Does diff work on PDFs or images?
No, diff is for text files. For binary files, use cmp to check if they are identical, or use sha256sum to compare checksums. For images, you can use ImageMagick’s compare command to generate a visual difference image.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.