Home Learn Linux How to compare two files in Linux using terminal commands

How to compare two files in Linux using terminal commands

We will cover the diff and cmp commands, along with tips and tricks to make the comparison process easier. We will also share troubleshooting tips to help you avoid common issues when comparing files in Linux.

by Divya Kiran Kumar
compare files linux

Comparing two files in Linux can be helpful when you want to identify the differences between two files. In the Terminal, several commands can be used to compare files, such as diff and cmp. In this guide, we will go over how to compare two files using these commands and provide some tips and tricks to make the process easier.

Why compare files in terminal

There are several reasons why you might want to compare two files in the Terminal:

  • Debugging: When working on a project and having multiple versions of a file, comparing the differences can help you debug issues that may have occurred.
  • Checking for changes: If you have made changes to a file, comparing it to the original file can help you see what has been changed.
  • Verifying backups: If you have backed up a file and want to confirm that the backup is accurate, comparing the original file to the backup can help you confirm that they are the same.

Comparing two files in Linux

1. Using the diff command

The diff command is used to compare two files line by line.

Open the Terminal.
Type the following command:

diff file1 file2

Replace file1 and file2 with the names of the files you want to compare. Below is a screenshot showing our example. We are comparing two files, FOSSLinux_Sample_1.c and FOSSLinux_Sample_2.c

diff FOSSLinux_Sample_1.c FOSSLinux_Sample_2.c
using diff command

Using diff command

Press Enter.

By comparing the differences between these two versions of the code, you can quickly identify the change that was made, which in this case is the value of y. This can be especially useful when working with larger, more complex codebases where it may be difficult to identify changes by manually reviewing the code.

The output will show the differences between the two files, with additions and deletions marked with + and – signs, respectively.

2. Using the cmp command

The cmp command is used to compare two files byte by byte.

Open the Terminal.
Type the following command:

cmp file1 file2

Replace file1 and file2 with the names of the files you want to compare.

In our example, we will run the following command:

cmp FOSSLinux_Sample_1.c FOSSLinux_Sample_2.c
cmp command usage

cmp command usage

Press Enter.

You can see the output showing difference in line 5.

The cmp command will not output anything if the files are the same. However, if the files are different, the cmp command will display the byte position of the first difference.

Tips & tricks

  • Ignore whitespace: If you want to ignore whitespace differences, use the -w option with the diff command:
diff -w file1 file2
  • Ignore case: If you want to ignore case differences, use the -i option with the diff command:
diff -i file1 file2
  • Compare directories: If you want to compare all the files in two directories, use the -r option with the diff command:
diff -r directory1 directory2
  • Redirect output: If you want to save the output of the diff or cmp command to a file, use the > operator to redirect the output:
diff file1 file2 > output.txt
  • Compare compressed files: If you want to compare two compressed files, use the zdiff or zcmp command for files compressed with gzip:
zdiff file1.gz file2.gz

For files compressed with bzip2, use bzdiff or bzcmp:

bzdiff file1.bz2 file2.bz2

By using these commands and tips, comparing two files in Linux becomes a straightforward process that can help you debug issues, track changes, and verify backups.

Troubleshooting tips

Here are some troubleshooting tips that may be helpful when comparing two files in Linux:

  • Check file permissions: Ensure you have the necessary permissions to access the files you want to compare. You may encounter errors when comparing the files if you don’t have the required permissions.
  • Check file types: Make sure the files you want to compare are the same type. For example, using the diff command, you can’t compare a binary file with a text file.
  • Check file encoding: If comparing text files, ensure they are encoded similarly. For example, if one file is encoded in UTF-8 and the other in ISO-8859-1, you may encounter errors when comparing the files.
  • Check file size: If the files you want to compare are huge, the process may take a long time or cause your system to run out of memory. Consider using the head or tail command to preview the files before comparing them.
  • Check file format: If you compare files created on different operating systems, you may encounter issues with line endings. Windows uses a different line ending than Linux and macOS, which can cause differences when comparing text files. Consider using the dos2unix or unix2dos command to convert the line endings before comparing the files.

By keeping these troubleshooting tips in mind, you can avoid common issues when comparing files in Linux and ensure the comparison process goes smoothly.

Conclusion

Comparing two files in Linux can be a powerful way to identify their differences, check for changes, and verify backups. Several commands can be used for file comparisons in the Terminal, such as diff and cmp. The diff command is used to compare two files line by line, while the cmp command is used to compare two files byte by byte.

In addition, tips and tricks can make the comparison process easier, such as ignoring whitespace, ignoring case, and redirecting output. Troubleshooting tips can also help to avoid common issues when comparing files in Linux, such as file permissions, file types, file encoding, file size, and file format. We hope you enjoyed the article.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.