Linux gives you more ways to compare files than almost any other operating system — a handful of battle-tested terminal tools built over 50 years, plus modern GUI applications and browser-based options. Whether you need to linux compare two files line by line, check whether two binaries are byte-for-byte identical, find the difference between sorted lists, or visualize a diff in a full color-coded GUI, there is a purpose-built tool for the job. This guide covers all eight methods — with real commands, real output, and a decision matrix to help you pick the right one every time.

terminal $ diff -u config.old config.new --- config.old 2026-04-08 +++ config.new 2026-04-09 @@ -3,5 +3,5 @@ database: - host: db.prod.example.com + host: db.staging.example.com port: 5432 name: appdb ssl: true exit code: 1 (files differ) Meld — config.old vs config.new config.old config.new database: database: db.prod.example.com db.staging.example.com port: 5432 port: 5432 name: appdb name: appdb ssl: true ssl: true Terminal (diff -u) GUI (Meld) vs

Why Compare Files in Linux

File comparison in Linux is not a single-use skill — it underpins almost everything developers and system administrators do daily. Here is why it matters across different roles:

  • Developers: Every pull request is a diff. Code review, merge conflict resolution, and understanding what changed between releases all rely on file comparison linux tools under the hood.
  • DevOps & SREs: Configuration drift is a leading cause of production incidents. Comparing nginx.conf between servers, or the current .env against a snapshot taken at deploy time, catches silent mismatches before they cause outages.
  • Data engineers: When a pipeline produces different output than expected, diffing the old and new files immediately scopes the problem.
  • Security analysts: Comparing a binary against a known-good checksum, or diffing two versions of a script to identify injected code, is a core forensics technique. Static code analysis tools automate this at scale in CI pipelines.
  • Power users: Duplicate file detection, backup verification, and syncing directories all involve linux compare 2 files or comparing entire directory trees. On Windows, the same workflow uses Robocopy and PowerShell — see how to compare Windows folders.

The tools covered below range from the famous diff (available on every Linux system) to polished GUI applications. Knowing how to linux compare files with the right tool — and why — is the practical skill this guide develops.

Use Case → Recommended Tool Linux File Comparison Code review / patch → diff Binary check / identity → cmp List set ops / unique lines → comm Side-by-side terminal → sdiff Vim users / SSH → vimdiff Visual GUI / merge → Meld Each use case has a purpose-built tool in the Linux toolkit

Method 1: diff — Line-by-Line Comparison

diff is the canonical tool for linux diff two files. It compares text files line by line and outputs a compact description of every addition, deletion, and change. Part of the GNU diffutils package, its output format is the foundation of git diff, GitHub pull request views, and patch files distributed across the open-source ecosystem.

For a deep dive into diff flags, output formats (normal, unified, context, side-by-side), and scripting patterns, see our dedicated diff command in Linux guide. This section covers the essential patterns you need day to day.

Basic Usage

diff file1.txt file2.txt

Exit code 0 means the files are identical. Exit code 1 means they differ. Exit code 2 means an error (file not found, permission denied). This exit-code convention makes diff the most common way to linux compare two files inside shell scripts.

The Three Most Useful Flags

Unified format (-u) — the modern standard. Shows three lines of context around each change, uses - for removed lines and + for added lines. This is the format that git diff produces.

diff -u config.prod.yaml config.staging.yaml

Sample output:

--- config.prod.yaml    2026-04-08 10:12:00
+++ config.staging.yaml 2026-04-08 10:15:00
@@ -3,7 +3,7 @@
 database:
-  host: db.prod.example.com
+  host: db.staging.example.com
   port: 5432
   name: appdb

Side-by-side (-y) — prints both files in two columns with a separator in the middle (| for changed, < for left-only, > for right-only). Add --suppress-common-lines to hide identical lines.

diff -y --suppress-common-lines file1.txt file2.txt

Recursive directory comparison (-r) — compares all files in two directories and their subdirectories. Combine with -q (brief) to see only which files differ, without the line-level content.

# Show only which files differ
diff -rq project-v1/ project-v2/

# Full unified diff of all changes across directories
diff -ru project-v1/ project-v2/

Ignore Whitespace

# Ignore all whitespace differences
diff -w file1.txt file2.txt

# Ignore only changes in whitespace amount (not all whitespace)
diff -b file1.txt file2.txt

# Ignore blank line differences
diff -B file1.txt file2.txt

Whitespace-ignoring flags are especially useful when comparing code that was reformatted or YAML/JSON that was re-indented without semantic changes.

Anatomy of a Unified Diff (diff -u) --- config.prod.yaml 2026-04-08 +++ config.staging.yaml 2026-04-09 @@ -3,7 +3,7 @@ database: port: 5432 - host: db.prod.example.com + host: db.staging.example.com name: appdb ssl: true timeout: 30 file headers --- old / +++ new hunk header @@ removed (- red) added (+ green) context (grey) git diff and patch files use this exact format

Method 2: cmp — Byte-Level Binary Comparison

While diff works line by line on text, cmp works byte by byte on any file type. When you linux compare 2 files that are binary — executables, images, archives, compiled libraries — cmp is the right tool. It reports the exact byte position of the first difference rather than a description of what changed.

Basic Usage

cmp file1 file2

If the files are identical, cmp exits with code 0 and produces no output. If they differ, it reports the byte offset and line number of the first difference:

cmp image_v1.png image_v2.png
# Output: image_v1.png image_v2.png differ: byte 4928, line 1

Useful cmp Flags

Flag Effect Example
-l Print byte number and differing byte values for every difference (verbose) cmp -l file1 file2
-s Silent — exit code only, no output. Ideal for scripts. cmp -s file1 file2 && echo "identical"
-i N Skip the first N bytes of both files before comparing cmp -i 512 disk1.img disk2.img
-n N Compare at most N bytes cmp -n 1024 file1 file2

Comparing Binary Files with Hex Dumps

When you need a full byte-by-byte view of differences in binary files, combine xxd and diff:

diff <(xxd binary_v1) <(xxd binary_v2)

This produces a unified diff of the hex representation, making it easy to see which bytes changed and in what context.

diff vs cmp — Key Distinction

Use diff when you need to understand what changed in a text file. Use cmp when you need to know whether two files are identical at the byte level, or when the files are binary. cmp -s is often the fastest way to check file identity in a bash script:

This pattern is one of the simplest ways to bash diff two files inside an automation script:

if cmp -s "$BACKUP" "$CURRENT"; then
  echo "No changes since last backup"
else
  echo "File has changed — running backup"
  cp "$CURRENT" "$BACKUP"
fi

Method 3: comm — Find Common and Unique Lines

comm is the least-known of the three classic file comparison tools, but it solves a specific problem that diff handles awkwardly: finding lines that are common to both files, unique to file 1, or unique to file 2 — in sorted order.

Important requirement: comm requires both files to be sorted. If your files are not sorted, pipe them through sort first.

Basic Usage

comm file1_sorted.txt file2_sorted.txt

The output has three tab-indented columns:

  • Column 1 (no indent): Lines only in file1
  • Column 2 (one tab): Lines only in file2
  • Column 3 (two tabs): Lines in both files
# Example: two user lists
comm users_monday.txt users_friday.txt

# Sample output:
alice           # only in Monday
        bob     # only in Friday
                carol   # in both
                dave    # in both
eve             # only in Monday

Suppressing Columns

Use -1, -2, and -3 flags to suppress individual columns. This makes it easy to extract exactly the set you need:

# Lines only in file1 (not in file2)
comm -23 file1_sorted.txt file2_sorted.txt

# Lines only in file2 (not in file1)
comm -13 file1_sorted.txt file2_sorted.txt

# Lines common to both files
comm -12 file1_sorted.txt file2_sorted.txt

Real-World Example: Set Operations on Lists

# Find IPs that are in the blocklist but NOT in the allowlist
comm -23 <(sort blocklist.txt) <(sort allowlist.txt)

# Find packages installed on server A but not server B
comm -23 <(dpkg --get-selections | awk '{print $1}' | sort) \
         <(ssh serverB "dpkg --get-selections | awk '{print $1}'" | sort)

This is exactly the use case covered in our guide on how to compare two listscomm is the command-line equivalent of the "find items in A but not B" operation.

Method 4: sdiff — Side-by-Side Terminal View

sdiff (side-by-side diff) is a purpose-built tool for terminal compare in a two-column layout. It is similar to diff -y but with richer interactive merge capabilities.

sdiff Side-by-Side Output $ sdiff -s file1.txt file2.txt file1.txt file2.txt sep host: db.prod.com host: db.prod.com port: 5432 | port: 3306 name: appdb name: appdb ssl: true < > ssl: false timeout: 30 timeout: 30 | = lines differ < = left file only > = right file only = identical Use -s to suppress common lines; -o merged.txt to interactively merge

Basic Usage

sdiff file1.txt file2.txt

The output format uses these separators:

Separator Meaning
(space) Lines are identical
| Lines are different
< Line exists only in the left file
> Line exists only in the right file

Key Flags

# Show only differing lines (suppress common lines)
sdiff -s file1.txt file2.txt

# Set column width (default is terminal width)
sdiff -w 120 file1.txt file2.txt

# Interactive merge: write merged output to a third file
sdiff -o merged.txt file1.txt file2.txt

The -o flag is what makes sdiff unique: it launches an interactive merge session where you choose which version of each differing line to keep in the output file. For each difference, sdiff prompts you with options:

# sdiff interactive merge prompts
%  l    # use left version
%  r    # use right version
%  s    # silently include common lines
%  v    # edit the line in $EDITOR

sdiff vs diff -y

diff -y and sdiff produce similar output, but sdiff has the interactive merge feature and slightly cleaner column alignment by default. For a quick terminal compare, either works. For merging without a GUI, sdiff -o is the terminal-native solution.

Method 5: vimdiff — Compare Inside Vim

vimdiff opens two (or more) files in Vim's diff mode — a split-screen view with color highlighting, change navigation, and merge capabilities, all inside the terminal. If you need to linux diff two files while editing them simultaneously, vimdiff is the go-to for developers already living in Vim who want a richer experience than plain diff output.

Opening vimdiff

# Two files
vimdiff file1.txt file2.txt

# Three-way diff (base + two modified versions)
vimdiff base.txt modified_a.txt modified_b.txt

# From inside Vim
:diffsplit file2.txt

Essential vimdiff Navigation

Keymap Action
]c Jump to next change
[c Jump to previous change
do Diff obtain — pull the change from the other window into this one
dp Diff put — push this change to the other window
:diffupdate Refresh diff highlighting after manual edits
zo / zc Open / close folded identical sections
:only Close all windows except the current one

Using vimdiff as Git's Merge Tool

You can configure Git to use vimdiff for merge conflicts:

git config --global merge.tool vimdiff
git config --global mergetool.vimdiff.layout "LOCAL,BASE,REMOTE / MERGED"

# Resolve a merge conflict
git mergetool

This opens a four-panel view: LOCAL (your changes), BASE (common ancestor), REMOTE (incoming changes), and MERGED (the output you are building). It is a powerful setup that requires no GUI tools whatsoever.

vimdiff: Four-Panel Merge View (git mergetool) LOCAL BASE REMOTE MERGED (output) host: db.prod.com host: db.prod.com host: db.prod.com host: db.prod.com port: 5432 port: 5000 port: 3306 port: ??? name: appdb name: appdb name: appdb name: appdb ssl: true ssl: true ssl: false ssl: ??? -- NORMAL -- [c next ]c prev do obtain dp put :diffupdate Resolve conflicts with do (obtain) / dp (put) then :wqa to finish

Method 6: Meld — Visual GUI Diff Tool

Meld is the most popular free, open-source GUI diff tool for Linux. It provides a color-coded side-by-side view, directory comparison with a file tree, and three-way merge support — all in a clean GTK interface. If you regularly need to linux compare two files visually and merge results, Meld is the best starting point.

Installation

# Debian/Ubuntu
sudo apt install meld

# Fedora/RHEL
sudo dnf install meld

# Arch Linux
sudo pacman -S meld

# Flatpak (universal)
flatpak install flathub org.gnome.meld

Basic Usage

# Compare two files
meld file1.txt file2.txt

# Compare two directories
meld dir1/ dir2/

# Three-way merge
meld base.txt modified_a.txt modified_b.txt

Key Meld Features

  • Inline highlighting: Changed characters within a line are highlighted, not just the entire line — making it easy to spot a single word change in a long line.
  • Directory mode: Shows a file tree with color-coded status (identical, modified, only in one side). Double-clicking opens the file diff.
  • Change navigation: Arrow buttons in the toolbar scroll to the next or previous change block. Each change can be merged individually with a single click.
  • Git integration: Meld detects version control repositories and can show working-tree changes against HEAD.
  • Auto-merge: For three-way merges, Meld can automatically resolve non-conflicting changes, leaving only genuine conflicts for manual review.

Meld as Git's Diff and Merge Tool

git config --global diff.tool meld
git config --global merge.tool meld

# Open a visual diff of staged changes
git difftool --staged

# Resolve merge conflicts visually
git mergetool

Method 7: KDiff3 and Beyond Compare

Beyond Meld, several other GUI tools are worth knowing for specific use cases in file comparison linux workflows.

KDiff3

KDiff3 is a free, open-source Qt-based diff and merge tool that excels at three-way merges. It handles large files well and has robust auto-merge capabilities.

# Install KDiff3
sudo apt install kdiff3      # Debian/Ubuntu
sudo dnf install kdiff3      # Fedora

# Compare two files
kdiff3 file1.txt file2.txt

# Three-way merge
kdiff3 base.txt file1.txt file2.txt -o merged.txt

KDiff3 strengths compared to Meld:

  • Better at auto-merging in three-way scenarios with complex overlaps
  • Handles very large files (50,000+ lines) without performance degradation
  • More configurable whitespace handling
  • Built-in directory comparison with recursive auto-merge

Beyond Compare

Beyond Compare is a paid, cross-platform diff tool ($35 Standard / $70 Pro) with a native Linux version. It supports not only text files but also binary hex comparison, image comparison, archive inspection (ZIP, tar, JAR), FTP/SFTP directory sync, and MP3 tag comparison.

# After downloading the .deb package
sudo dpkg -i bcompare-*_amd64.deb

# Compare two files
bcompare file1.txt file2.txt

# Compare two directories
bcompare dir1/ dir2/

Beyond Compare is worth the price for teams that routinely compare large codebases, sync directories over SSH, or need to diff archive contents without extracting them. For developers on multiple operating systems, a single license covers macOS, Windows, and Linux. Those on macOS can find similar alternatives covered in our Mac directory comparison guide.

Other Notable Tools

  • diffoscope: Designed for deep structural comparison of packages, binaries, and file archives. Used by the Reproducible Builds project to verify that compiler output is deterministic. Install via pip install diffoscope or apt install diffoscope.
  • xxdiff: A lightweight X11 GUI tool — minimal dependencies, fast startup, good for quick visual checks without installing a full application.
  • VS Code diff view: If VS Code is installed, code --diff file1.txt file2.txt opens a polished inline diff. Our VS Code file comparison guide covers every method, including the Command Palette workflow.

Comparison Matrix: Which Tool for Which Job

No single tool wins in all scenarios. Use this matrix to match the right tool to your specific situation when you need to linux compare files.

Which Tool Should I Use? Compare files Binary or text file? binary cmp byte-level text Comparing directories? yes diff -rq or Meld dir mode no GUI or terminal? GUI Meld or KDiff3 terminal side-by-side or unified? sdiff / diff -y diff -u / vimdiff
Tool Best For Binary Files Directories GUI Merge Cost
diff Text files, scripts, configs, patch generation No Yes (-r) No Via patch Free (built-in)
cmp Binary files, fast identity check, byte offset Yes No No No Free (built-in)
comm Set operations on sorted line lists No No No No Free (built-in)
sdiff Side-by-side terminal view, interactive merge No No No Yes (-o) Free (built-in)
vimdiff Vim users, SSH sessions, three-way merge No No Terminal Yes Free (built-in)
Meld GUI diff + directory comparison + Git integration No Yes Yes Yes Free (open-source)
KDiff3 Three-way merge, large files, auto-merge No Yes Yes Yes Free (open-source)
Beyond Compare Binary + archive + FTP + cross-platform power users Yes Yes Yes Yes $35–$70

Choose by Use Case

  • "Are these two files identical?"cmp -s file1 file2 (works for any file type, exit code only)
  • "What exactly changed between these two configs?"diff -u old.conf new.conf
  • "Show me the differences side by side in the terminal."sdiff -s file1 file2 or diff -y --suppress-common-lines file1 file2
  • "I want to compare and merge from inside my editor."vimdiff file1 file2
  • "Which files differ between these two directories?"diff -rq dir1/ dir2/
  • "I need a visual GUI for code review or merge resolution." → Meld (free) or Beyond Compare (paid)
  • "I need to compare lists and find items in one but not the other."comm -23 <(sort list1.txt) <(sort list2.txt)
  • "I need to compare binary or archive files."cmp -l, xxd + diff, or Beyond Compare

When a Browser Extension Beats the Terminal

The terminal tools above are powerful ways to linux compare files, but they have a real limitation: they are optimized for files you already have locally. When you need to compare text that came from a web page, an API response, a clipboard paste, a .docx export, or a spreadsheet, opening a terminal adds friction. Trying to spot the difference by eye across browser tabs is error-prone — automation catches what humans miss.

A browser extension solves this class of problem directly. The Diff Checker extension runs in Chrome on any Linux desktop — no package manager, no dependencies, no version conflicts. You paste or upload the two pieces of content and see a color-coded diff instantly.

What the Extension Offers

  • Split view and unified view — the same two formats you get from diff -y and diff -u, but visual and color-coded
  • Three comparison algorithms: Smart Diff (semantic), Ignore Whitespace, and Legacy LCS — choose based on whether whitespace matters
  • Syntax highlighting via Monaco Editor for JavaScript, TypeScript, Python, Java, C, C++, Go, Ruby, PHP, HTML, CSS, JSON, YAML, SQL, and more
  • File upload — drag in text files, .docx, or .xlsx files directly
  • Format Code — Prettier-based formatting for 20+ languages, so you can normalize formatting before diffing
  • Normalize — sort JSON keys, sort CSS properties, strip excess whitespace
  • Compare browser tabs — diff the text content of two open tabs directly
  • History with auto-save — previous comparisons are preserved locally
  • Optional AI Diff Summary — uses your own OpenAI key to explain changes in plain English
  • Dark mode, word wrap, collapse unchanged sections

Try Diff Checker — Free Chrome Extension

Paste, upload, or drag in any two pieces of text and see a color-coded diff instantly. Syntax highlighting for 20+ languages, smart diff algorithms, file upload, and optional AI summaries. No terminal required — works on any Linux desktop with Chrome.

Add to Chrome — Free

Frequently Asked Questions

How do I compare two files in Linux without using diff?
Several alternatives work well. Use cmp file1 file2 for byte-level comparison (works on binaries). Use comm for set-style comparison of sorted line lists. Use sdiff file1 file2 for a side-by-side terminal view. For a GUI approach, Meld (sudo apt install meld) or the Diff Checker browser extension both work without touching the terminal.
What's the difference between diff and cmp?
diff compares text files line by line and reports which lines changed — ideal for source code, configs, and any human-readable text. At its core, each line comparison is a string compare operation. cmp compares any two files byte by byte and reports the byte position of the first difference — ideal for binary files (images, executables, archives) or when you only need to know whether files differ, not how.
How do I compare files side-by-side in Linux terminal?
Two options: sdiff file1.txt file2.txt produces a two-column view with | for changed lines. Alternatively, diff -y file1.txt file2.txt does the same within the standard diff tool. Add --suppress-common-lines (or sdiff -s) to hide identical lines and focus on differences.
Can I visually compare files in Linux GUI?
Yes. Meld is the best free option — install with sudo apt install meld or sudo dnf install meld. It supports file comparison, directory trees, and three-way merges. KDiff3 is another strong free option. Beyond Compare is paid but provides the broadest file-type support including binaries and archives. On Windows, Notepad++ with the Compare plugin is the lightweight alternative. For quick no-install comparisons, the Diff Checker Chrome extension runs on any Linux desktop.
What tools compare binary files in Linux?
cmp is the standard — it reports the byte offset of the first difference and works on any file type. For a full hex view, use diff <(xxd file1) <(xxd file2). For deep structural comparison of packages, ELF binaries, or archives, diffoscope provides the most detail. Beyond Compare also has a hex comparison view.
How do I compare directories recursively in Linux?
Run diff -rq dir1/ dir2/ to see which files differ without the line-level content. Use diff -ru dir1/ dir2/ for a full unified diff of all changes. Exclude files with -x PATTERN: diff -rq -x ".git" -x "node_modules" dir1/ dir2/. For a GUI tree view, Meld's directory mode is the most user-friendly option.
What is the fastest way to bash diff two files in a script?
For a yes/no check: if diff -q file1 file2 >/dev/null 2>&1; then — this suppresses all output and uses the exit code. For binary files: cmp -s file1 file2 is slightly faster. To capture the diff output for logging: DIFF=$(diff -u file1 file2), then check if [ -n "$DIFF" ].