Comparing text files is a common task for Linux users, developers, system administrators, and anyone working with configuration files, code, or large datasets. Whether you’re checking changes between two versions of a document, identifying differences in configuration files, or troubleshooting an application, Linux provides several powerful tools to make the process easier.

If you’re wondering how to compare two text files in Linux, the simplest solution is to use the diff command. It compares two files line by line and displays their differences directly in your terminal.

However, diff isn’t your only option. Linux also offers tools such as cmp, comm, vimdiff, and graphical applications for different comparison needs.

In this guide, you’ll learn how to compare two text files in Linux using practical commands, understand their output, ignore unnecessary differences, and choose the right method for your workflow.

Quick Answer: How to Compare Two Text Files in Linux

Open your terminal and run:

diff file1.txt file2.txt

If both files contain identical text, the command produces no output.

If differences exist, diff displays the changes and their locations.

For an easier-to-read comparison, use:

diff -u file1.txt file2.txt

For a side-by-side comparison, run:

diff -y file1.txt file2.txt

These three commands cover many everyday text comparison tasks in Linux.

1. Why Do You Need to Compare Text Files in Linux?

Text file comparison helps identify changes between two files without manually reading every line.

For example, imagine you’re managing a Linux web server and have two versions of an Nginx configuration file. One version works correctly, while the other causes problems.

Instead of opening both files and searching manually, you can compare them to identify the changes.

Common situations include:

  • Configuration management: Compare server settings before and after an update.
  • Software development: Identify changes between versions of source code.
  • Data verification: Check whether two exported datasets contain the same information.
  • Document editing: Find added, removed, or modified text.
  • Backup verification: Determine whether backup files match their originals.
  • Troubleshooting: Find unexpected changes that might explain an error.

Linux comparison tools can identify differences in seconds, even when manually reviewing the files would be difficult.

2. How to Compare Two Text Files in Linux Using the diff Command

The diff command is one of the most commonly used Linux utilities for text comparison. It identifies line-level changes and can present them in several formats.

Step 1: Open the terminal

Open your Linux terminal using your desktop’s application menu or terminal shortcut.

Navigate to the directory containing your files:

cd /home/user/Documents

Replace this example path with the actual location of your files.

You can check the available files using:

ls

Step 2: Create two sample text files

For this tutorial, create a file named file1.txt:

printf 'Apple\nBanana\nCherry\n' > file1.txt

Create a second file named file2.txt:

printf 'Apple\nBlueberry\nCherry\nDate\n' > file2.txt

The first file contains:

Apple
Banana
Cherry

The second contains:

Apple
Blueberry
Cherry
Date

Notice that the second line has changed and an additional line appears at the end.

Step 3: Run the diff command

Execute:

diff file1.txt file2.txt

Expected output:

2c2
< Banana
---
> Blueberry
3a4
> Date

The command has identified both changes.

Step 4: Understand the output

The standard diff output uses special symbols to represent changes.

SymbolMeaning
<A line from the first file
>A line from the second file
aAdded lines
dDeleted lines
cChanged lines
---Separates old and new text in a change

In our example, 2c2 means line 2 in the first file was changed to line 2 in the second file.

Similarly, 3a4 indicates that line 4 of the second file was added after line 3 of the first file.

When to use it: Choose standard diff when you need a quick, detailed comparison directly in the terminal.

3. Compare Text Files in Linux Side by Side

Sometimes, seeing both files next to each other makes differences easier to understand.

GNU diff supports this using the -y option.

Run:

diff -y file1.txt file2.txt

The output will resemble:

Apple       Apple
Banana    | Blueberry
Cherry      Cherry
          > Date

The actual spacing depends on your terminal and the selected output width.

Here is what the symbols mean:

  • | indicates that corresponding lines differ.
  • < indicates that a line exists only in the first file.
  • > indicates that a line exists only in the second file.

To adjust the output width, use:

diff -y -W 100 file1.txt file2.txt

This limits the entire output to approximately 100 columns.

You can also hide identical lines:

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

This displays only the differences, making long-file comparisons easier to review.

The side-by-side options are documented in the GNU Diffutils manual.

When to use it: Side-by-side comparison is particularly useful when reviewing documents, configuration files, or two versions of source code.

4. Compare Two Text Files Using Unified Diff Format

Unified diff displays changes together with surrounding lines, making it easier to understand what changed and where.

Run:

diff -u file1.txt file2.txt

Example output:

--- file1.txt
+++ file2.txt
@@ -1,3 +1,4 @@
 Apple
-Banana
+Blueberry
 Cherry
+Date

The output uses a simple notation:

SymbolDescription
---Original file header
+++Updated file header
-Removed line
+Added line
SpaceUnchanged line
@@Location of the changes

The file headers normally include modification timestamps, which have been omitted from this simplified example.

The line:

@@ -1,3 +1,4 @@

indicates that the displayed section contains three lines from the first file and four from the second.

Unified format includes three lines of context by default. You can change that using -U, followed by the required number of context lines.

For example:

diff -U 5 file1.txt file2.txt

This requests five lines of surrounding context where available.

When to use it: Unified format works well for code reviews, software patches, configuration audits, and sharing changes with other developers.

5. Compare Two Text Files in Linux While Ignoring Whitespace

Whitespace differences can make two otherwise similar files appear different.

For example:

Hello World

And:

Hello    World

The actual words are identical, but the amount of spacing differs.

Linux provides several options for handling whitespace.

Ignore all whitespace

Run:

diff -w file1.txt file2.txt

The -w option ignores whitespace differences within lines, even when whitespace is present in one version but missing in the other.

Ignore changes in the amount of whitespace

Use:

diff -b file1.txt file2.txt

This treats sequences of one or more whitespace characters as equivalent but can still detect a difference between a space and no space.

Ignore blank lines

Run:

diff -B file1.txt file2.txt

This ignores changes consisting entirely of blank lines.

Ignore trailing whitespace

Use:

diff -Z file1.txt file2.txt

This ignores whitespace at the end of lines.

You can combine options:

diff -u -w -B file1.txt file2.txt

This produces unified output while ignoring whitespace differences and changes consisting only of blank lines.

Important: Ignoring whitespace can conceal meaningful changes in formats such as Python, where indentation affects code behavior. Also, -w does not ignore the line breaks that separate lines.

6. Compare Two Text Files Without Case Sensitivity

By default, diff distinguishes uppercase and lowercase letters.

For example:

Linux

And:

linux

These are normally treated as different.

To ignore capitalization, use:

diff -i file1.txt file2.txt

The -i option tells GNU diff to ignore case differences.

You can combine case-insensitive comparison with other options:

diff -i -w file1.txt file2.txt

This ignores both capitalization and whitespace differences.

Case-insensitive matching is useful when comparing keyword lists, product names, or text where capitalization is not important.

However, it does not perform full Unicode case folding, so certain characters and language-specific capitalization rules may still produce differences.

7. Check Whether Two Files Are Identical

If you only want to know whether two files match, there’s no need to display every difference.

Use:

diff -q file1.txt file2.txt

If they differ, you’ll see a message such as:

Files file1.txt and file2.txt differ

If they are identical, the command normally produces no output.

You can also display a status message using a shell script:

if diff -q file1.txt file2.txt > /dev/null; then
    echo "Files are identical"
else
    echo "Files differ or an error occurred"
fi

For more robust automation, check the command’s exit status. GNU diff returns 0 for identical files, 1 for differences, and 2 for an error.

8. Compare Two Files Byte by Byte Using cmp

The cmp command compares files byte by byte rather than line by line.

Run:

cmp file1.txt file2.txt

Example output:

file1.txt file2.txt differ: byte 7, line 2

This identifies the first differing byte and its location.

To perform a silent comparison:

cmp -s file1.txt file2.txt

Use the exit status to determine whether the files match.

Unlike a text-oriented comparison, cmp checks exact byte equality, making it useful for verifying copies and detecting differences that a whitespace-ignoring comparison would miss.

9. Find Unique and Common Lines Using comm

The comm command identifies lines unique to each file and lines shared by both.

Important: The input files must be sorted using the same sorting rules before comparison.

For a simple comparison, run:

sort file1.txt > sorted1.txt
sort file2.txt > sorted2.txt

comm sorted1.txt sorted2.txt

To display only the lines unique to the first file:

comm -23 sorted1.txt sorted2.txt

To display lines unique to the second file:

comm -13 sorted1.txt sorted2.txt

To display common lines:

comm -12 sorted1.txt sorted2.txt

For all noncommon lines, use:

comm -3 sorted1.txt sorted2.txt

Unlike diff, comm compares sorted sequences rather than identifying edits in their original positions.

This is useful for comparing lists of URLs, usernames, keywords, or inventory records.

10. Compare Two Text Files Visually Using vimdiff

If you’re comfortable with the Vim editor, vimdiff provides an interactive comparison environment.

Run:

vimdiff file1.txt file2.txt

Both files open in separate windows, with differences highlighted.

Useful keyboard commands include:

CommandAction
]cJump to the next difference
[cJump to the previous difference
doGet a change from the other window
dpSend a change to the other window
:qaQuit all windows

Be careful when using do and dp, as they can modify file contents in the editor.

For users who prefer a graphical interface, applications such as Meld provide another option for visual file comparison.

11. Compare Text Files Using Git Diff

Git provides another powerful way to compare files, even if they are not part of a Git repository.

Run:

git diff --no-index file1.txt file2.txt

The command generates a patch-style comparison between the two files.

The --no-index option explicitly tells Git to compare the supplied filesystem paths.

Git diff is especially useful for developers who already work with version control or want to examine changes in a familiar format.

12. Compare Files in Different Directories

The files don’t need to be in the same folder.

Specify their complete paths:

diff /home/user/old/config.txt \
     /home/user/new/config.txt

If a path contains spaces, place it inside quotation marks:

diff "Old Files/config.txt" "New Files/config.txt"

You can also compare entire directories recursively:

diff -r directory1 directory2

For a brief summary:

diff -qr directory1 directory2

This helps identify changed or missing files across directory structures.

13. Save Text Comparison Results to a File

Sometimes you need to save the results for documentation, troubleshooting, or future reference.

Use output redirection:

diff -u file1.txt file2.txt > differences.txt

The resulting file contains the unified diff output.

You can review it later:

cat differences.txt

Or open it in a terminal pager:

less differences.txt

For long files, saving the comparison prevents important differences from disappearing as you scroll through the terminal.

Remember that > overwrites the destination file if it already exists. Use a new filename when you need to preserve previous reports.

14. Common Linux File Comparison Problems and Solutions

Problem: No output appears

The files may be identical.

Run:

diff -s file1.txt file2.txt

With GNU diff, this explicitly reports identical files.

Problem: Every line appears different

The files might use different line endings.

Windows commonly uses CRLF line endings, while Linux typically uses LF.

For GNU diff, try:

diff --strip-trailing-cr file1.txt file2.txt

This removes trailing carriage returns from input lines during comparison without modifying the source files.

Problem: File not found

Verify that both paths are correct:

ls -l file1.txt file2.txt

Use absolute paths if the files are in different directories.

Problem: Permission denied

Check file permissions:

ls -l file1.txt file2.txt

Ensure your account has permission to read both files. Avoid changing permissions unnecessarily.

15. Which Linux Text Comparison Tool Should You Use?

Here’s a quick reference for selecting a method.

ToolPurpose
diffDisplay line-by-line differences
diff -uProduce patch-style output
diff -yCompare files side by side
diff -qCheck whether files differ
cmpCheck exact byte differences
commCompare common and unique sorted lines
vimdiffVisually review and edit differences
git diffReview changes in Git-style format

For most everyday comparisons, start with diff -u. It provides enough context to understand what changed without requiring additional software.

Frequently Asked Questions (FAQs)

How do I compare two text files in Linux?

Run diff file1.txt file2.txt in your terminal. The command displays differences between the two files line by line.

How can I compare two files side by side in Linux?

Use diff -y file1.txt file2.txt. This displays both files in separate columns and marks their differences.

How do I compare files without considering whitespace?

Run diff -w file1.txt file2.txt. This ignores whitespace differences within lines.

How do I check whether two files are exactly identical?

Use cmp -s file1.txt file2.txt and inspect the exit status. A status of 0 indicates byte-for-byte equality.

Can Linux compare large text files?

Yes. Linux utilities such as diff and cmp can compare large files. Performance depends on the files’ sizes, contents, available memory, and comparison method.

Conclusion

Learning how to compare two text files in Linux can make troubleshooting, programming, and system administration significantly easier.

The diff command is a practical starting point for identifying line-by-line changes. Its unified and side-by-side formats provide additional context, while cmp checks exact byte differences and comm identifies shared or unique lines in sorted files.

If you prefer an interactive interface, vimdiff offers visual comparison and editing capabilities.

Start with this command:

diff -u file1.txt file2.txt

It provides a clear overview of additions, deletions, and unchanged surrounding text, helping you understand exactly what has changed between your files.