How to Compare Two Files Linux [3 Effective Methods]

Written by

Reviewed by

Last updated: June 13, 2024

Expert verified

SVG Image

TL;DR

To compare two files Linux, follow these steps:

  1. Open your Terminal with Ctrl+Alt+T.
  2. Type diff file1.txt file2.txt and press Enter.
  3. Review the output to see the line-by-line differences between the files.

Read the article below to learn different methods to Linux compare two files, tips and tricks, and best practices.

Ever wondered if two files differ, ensure data integrity, or debug code issues? Comparing files in Linux can handle all these tasks. In this post, I’ll show you three effective methods to Linux compare files using the diff, cmp, and comm commands. You’ll also get tips for handling large files and best practices for regular comparisons. By the end, you’ll have the tools to compare files efficiently. Stick around, and let’s make file comparison in Linux easy and straightforward for you!

When You Need to Do File Comparison

File comparison is useful in many situations. Here are some common reasons you might need to compare files:

  1. Check for Differences: You want to see if two files are different. For example, you might compare two versions of a document to find changes.
  2. Verify Data Integrity: You need to ensure that a file hasn’t been altered. Comparing files helps confirm that copies or backups are identical to the original.
  3. Debugging and Troubleshooting: When you work with configuration files or code, comparing files can help identify issues by showing what has changed.
  4. Merging Changes: If multiple people work on the same file, comparing versions helps you merge their changes accurately.
  5. Identify Duplicates: Comparing files can help you find duplicate files in your system, which saves space and avoids confusion.
  6. System Administration: System admins often compare log files or configuration files to monitor changes and troubleshoot problems.

How to Use Diff Command to Compare Two Files Linux

To compare two files Linux using the diff command, open your Terminal and type diff file1.txt file2.txt. This will show you line-by-line differences between the files, highlighting any changes, additions, or deletions. It’s a powerful tool for pinpointing specific modifications and understanding exactly how two files differ.

Here are the detailed steps for three different methods to use diff command to Ubuntu compare two files:

1. Comparing Files Line by Line

In the line-by-line file comparison method, you can use the Linux diff command to analyze the differences between two files. This method is best used when you want a granular comparison of files line by line, enabling you to pinpoint specific modifications. Follow these steps:

  1. Open your Terminal window by pressing Ctrl+Alt+T.
opening terminal 12
  1. Enter the following command:
diff file1.txt file2.txt
  1. The output will be:
comparing two files in ubuntu
  1. 1,3c1,2

This line indicates that lines 1 to 3 in file1.txt have been changed and are now represented by lines 1 to 2 in file2.txt.

< editied file
< OS
< Ubuntu

These lines were present in file1.txt (lines 1 to 3), but they have been removed or modified in file2.txt.

This line separates the changes made in file1.txt from those made in file2.txt.

OS
Linux Ubuntu

These lines were added in file2.txt and are not present in file1.txt.

  1. 8a8,10

This line indicates that lines 8 to 10 in file2.txt have been added and are not present in file1.txt.

How did we choose these best Linux distros for beginners?

Is it hard to install applications on Linux?

Can you get more from Linux than Windows or MacOS?

These lines were added in file2.txt and are not present in file1.txt.

2. Comparing Files Word by Word

Comparing files word by word provides a more granular analysis, allowing you to track changes at a finer level. This method is particularly useful for text-based files where line-level comparison might not suffice. Here are the steps to compare two files Linux:

  1. Open your Terminal and enter the following command:
diff -w file1.txt file2.txt
  1. The -w flag ignores whitespace differences, allowing you to focus on word-level changes.
comparing files word by word
  1. 1d0

This line indicates that line 1 in data.txt has been deleted and is not present in essay.txt.

< editied file

This line was present in data.txt (line 1) but has been removed in essay.txt.

  1. 3c2

This line indicates that line 3 in data.txt has been changed and is now represented by line 2 in essay.txt.

< Ubuntu

This line was present in data.txt (line 3) but has been removed or modified in essay.txt.

This line separates the changes made in data.txt from those made in essay.txt.

dsada Ubuntu

This line was added in essay.txt and is not present in data.txt.

  1. 8a8,10

This line indicates that lines 8 to 10 in essay.txt have been added and are not present in data.txt.

3. Comparing Files Block by Block

Block comparison involves dividing files into chunks, comparing them individually, and presenting the differences. This method offers a broader perspective on file changes.

To compare files using a block-based approach:

  1. Open your Terminal and enter the following command:
diff -u file1.txt file2.txt
  1. The -u flag generates unified diff output, which presents the changes in a more readable and contextual format.
comparing files block by block
  1. These lines represent the changes between the compared files. Lines starting with a hyphen (-) indicate lines present in the original file (data.txt) but not in the modified file (essay.txt). Lines starting with a plus sign (+) indicate lines present in the modified file but not in the original file.

Other Commands to Compare Two Files in Linux

To compare two files Linux using comm and cmp commands, open your Terminal and type cmp file1.txt file2.txt for a byte-by-byte comparison. This will show the first difference between the files. For comparing sorted files line by line, use comm file1.txt file2.txt, which will display lines unique to each file and lines common to both. These tools help you identify differences efficiently.

Keep reading for the detailed step-by-step guide to compare two files:

1. cmp Command

The cmp command in Linux is a straightforward tool designed for comparing two files byte by byte. Its primary purpose is to identify whether two files are identical or where their first difference occurs. This tool is particularly useful for comparing binary files or for quickly determining if two files have any discrepancies.

  1. Most Linux distributions come with cmp as part of the coreutils package. However, if it is not installed, you can install it using the following commands:
sudo apt-get install coreutils
installing coreutils package on ubuntu
  1. To compare two files using cmp, run the command:
cmp file1.txt file2.txt

This command will compare file1.txt and file2.txt byte by byte.

  • If the files are identical, cmp will produce no output and the exit status will be 0.
  • If the files differ, cmp will output the byte and line number where the first difference occurs, and the exit status will be 1.
comparing files using cmp command

2. comm Command

The comm command in Linux is used to compare two sorted files line by line. It is an effective tool for identifying lines that are unique to each file or common to both. This makes comm particularly useful for tasks such as comparing sorted lists or datasets.

  1. Similar to cmp, comm is typically included in the coreutils package. To install it, use:
sudo apt-get install coreutils
  1. To compare two files using comm command, run:
comm file1.txt file2.txt

This command will compare file1.txt and file2.txt line by line. The output of comm is divided into three columns:

  • Lines unique to file1.txt.
  • Lines unique to file2.txt.
  • Lines common to both files.
comparing files using comm command

Tips for Comparing Large Files Efficiently

When comparing large files, it’s important to use methods that save time and resources. Here are some tips to help you compare large files more efficiently:

  • 🛠️ Use the Right Tool: Choose a tool like diff or cmp based on your needs. cmp is faster for binary files, while diff provides more detailed output for text files.
  • ✂️ Split Files: If a file is too large, split it into smaller parts using the split command. Compare the parts individually to avoid processing the entire file at once.
  • 💾 Limit Memory Usage: Use options that limit memory usage. For example, use diff --speed-large-files for large text files to speed up the comparison.
  • 🕒 Avoid Redundant Comparisons: Only compare files that have changed. Use timestamps or checksums to identify modified files before running a comparison.
  • ⚙️ Parallel Processing: Use parallel processing tools or scripts to compare multiple files simultaneously, reducing overall comparison time.

Best Practices for Regular File Comparisons in Projects

Regular file comparisons help maintain consistency and detect changes early in a project. Here are some best practices to follow:

  • 🤖 Automate Comparisons: Set up automated scripts using tools like cron to run comparisons at regular intervals. This ensures you don’t miss any changes.
  • 🗃️ Version Control Systems: Use version control systems like Git to track changes in your files. Git has built-in comparison tools that make it easy to see differences.
  • 📝 Document Differences: Keep a log of file comparison results, especially for critical files. Documenting differences helps in tracking changes and understanding their impact.
  • 🔍 Review and Merge Changes: Regularly review file comparison results to understand changes. Use tools like diff to merge changes carefully, especially in collaborative projects.
  • 📦 Backup Regularly: Make regular backups of your files before performing comparisons. This ensures you have a restore point in case something goes wrong during the comparison or merging process.

Compare Two Files in Linux: Wrapping Up

In this article, I showed you how to compare files Linux using diff, cmp, and comm. Each method is great for different needs, whether you’re looking at text, binary files, or sorted lines. I also gave tips for handling large files and best practices for regular comparisons, like automation and backups.

Frequently Asked Questions 

How can I exclude certain lines from comparison?

You can exclude specific lines from the comparison by utilizing the -I flag followed by a regular expression. For example, if you want to exclude lines starting with a hash symbol (#), you can use the command diff -I ^#.* file1.txt file2.txt. This regular expression pattern ^#.* matches any line that starts with a hash symbol, ensuring that those lines are excluded from the comparison. By leveraging the -I flag and a suitable regular expression, you can customize the comparison to exclude specific lines irrelevant to your analysis.

Can I use Diff to compare directories instead of files?

No, the Diff command is primarily used to compare individual files, not directories. If you attempt to compare directories using the Diff command directly, you will encounter an error or incorrect results. To compare directories, there are specialized tools available such as diff -r or rsync. These tools are specifically designed to handle directory comparisons. By utilizing the appropriate tools, you can accurately compare the contents of directories, including the files within them and any subdirectories.

How do I apply Diff recursively to compare subdirectories?

To apply the Diff command recursively and compare subdirectories, you can use the -r flag. For example, the command diff -r directory1 directory2 compares all files in directory1 and directory2, including their respective subdirectories. The recursive comparison ensures that all files and subdirectories within the specified directories are analyzed for differences. This can be useful when you want to compare directories and their contents comprehensively. By incorporating the -r flag into the Diff command, you can effectively compare subdirectories and gain insights into the differences at a deeper level.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Download and Install iTunes for Ubuntu [14 Easy Steps]

Next Post

How to Install Python3 on Ubuntu [3 Best Methods]

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next