In this chapter, we will learn how to compare files or folders in Linux Terminal. We will take a look at cmp, diff and comm tools.

How To Compare Files or Folders in Linux

As a beginner in the world of Linux, it’s essential to familiarize yourself with the tools available in the terminal to compare files. In this tutorial, we’ll cover two fundamental tools: cmp and diff. We’ll explore what each tool does, their typical use cases, and provide examples of how to use them in a Debian terminal. This section compliments the theme of the chapter, regular expressions in linux.

Cmp

cmp is a command-line tool used to compare two files byte by byte. It reports the differences between the two files, including the location and content of the differences.

Typical Use Cases

Example Usage

Open a Debian terminal and type:

$ cmp file1.txt file2.txt

This will compare the contents of file1.txt and file2.txt and report any differences.

Diff

diff is a command-line tool used to compare files or folders in Linux and report their differences in a human-readable format. It can be used to compare files line by line or character by character.

Typical Use Cases

  • Comparing two versions of a file to identify changes
  • Checking for differences between two files with the same name but different contents
  • Generating patch files to update one file to match another

Example Usage

Open a Debian terminal and type:

$ diff file1.txt file2.txt

This will compare the contents of file1.txt and file2.txt and report any differences in a human-readable format.

Advanced Use Cases:

  • Using cmp with options: cmp -l file1.txt file2.txt compares the files line by line and reports the differences.
  • Using diff with options: diff -u file1.txt file2.txt compares the files and reports the differences in a unified diff format.

Alternatives to cmp and diff

  • comm: a command-line tool used to compare two sorted files and report their differences.
  • sdiff: a command-line tool used to compare two files side by side and report their differences.
  • vimdiff: a command-line tool used to compare two files and report their differences in a visual format.

Use Cases of Alternatives

  • comm: typically used to compare two sorted files and report their differences, such as comparing two lists of names or numbers.
  • sdiff: typically used to compare two files side by side and report their differences, such as comparing two versions of a document.
  • vimdiff: typically used to compare two files and report their differences in a visual format, such as comparing two versions of a code file.
 
So far, we’ve covered the basics of comparing files in Linux terminal using cmp and diff. These tools are essential for identifying changes and differences between files, and are widely used in software development, data analysis, and system administration. By mastering these tools, you’ll be well on your way to becoming a proficient Linux user.

Tips and Tricks of using cmp or diff commands

  • Use cmp for byte-by-byte comparisons and diff for line-by-line comparisons.
  • Use diff with options to generate patch files or compare files in a unified diff format.
  • Use comm and sdiff for comparing sorted files and side-by-side comparisons, respectively.
  • Use vimdiff for visual comparisons of files.

Comm Command

Suppose we have two sorted files, file1.txt and file2.txt, containing the following contents:

file1.txt:
apple
banana
cherry
date

file2.txt:
apple
banana
elderberry
fig

To compare these two files using comm, we can use the following command:

$ comm file1.txt file2.txt

This will output the following:

              apple
              banana
cherry
              elderberry
              fig
date

The output shows the lines that are unique to each file, as well as the lines that are common to both files.

image for blog post Compare files or folders in Linux
Compare files or folders in Linux. AI generated image

Sdiff

Example:

Suppose we have two files, file1.txt and file2.txt, containing the following contents:

file1.txt:
This is the first line.
This is the second line.
This is the third line.

file2.txt:
This is the first line.
This is a different second line.
This is the third line.

To compare these two files using sdiff, we can use the following command:

$ sdiff file1.txt file2.txt

This will output the following:

This is the first line.                          This is the first line.
This is the second line.                         | This is a different second line.
This is the third line.                          This is the third line.

The output shows the lines that are common to both files, as well as the lines that are different.

Vimdiff

Example:

Suppose we have two files, file1.txt and file2.txt, containing the following contents:

file1.txt:
This is the first line.
This is the second line.
This is the third line.

file2.txt:
This is the first line.
This is a different second line.
This is the third line.

To compare these two files using vimdiff, we can use the following command:

$ vimdiff file1.txt file2.txt

This will open a visual diff interface in Vim, showing the differences between the two files. The interface will highlight the lines that are different, and allow us to navigate and edit the files. Tools like `diff` can generate patch files, useful for transforming one file into another.

Relevant Websites:

That’s it for this chapter 15, compare files or folders in Linux !


Previous: Chapter 28 | Next: Chapter 30