How to Use Diff to Compare Files in Linux

Diff is a command line tool for Unix systems that allows you to compare two files or directories and see the differences between them. It’s ideal for comparing old and new versions of files to see what’s changed. In this article we are going to show you how you can use diff to easily compare files in Linux.

1. Open a terminal window.

2. Type diff and the paths to two files you’d like to compare. In this case I’m comparing two small Python programs on my desktop, so I’m using the following command:

diff-terminal-1

3. Press Enter to submit the command. The resulting display will show you all the different lines between the files. Compared line numbers will be shown as “1c1,” meaning that the first line was compared to the first line, and the following differences were found.

diff-terminal-2

4. To see a side-by-side view of the files and their differences, use the -y flag after diff. For example:

diff-terminal-3

Any line with a pipe symbol ( | ) is a line with a difference. Just be sure to expand your terminal window first, or you might have a hard time seeing the display.

diff-terminal-4

The functionality of diff can also be accessed through Meld, a GUI for diff. Considering that diff’s user interface can be hard to get a handle on, Meld is a useful tool.

1. Install Meld from the command line by opening terminal and typing:

or

for Yum-based distro.

This will initialize the apt package manager and begin installing Meld. Note that the -y flag will automatically say “yes” to any prompts; please remove it if you’d rather confirm those prompts manually.

meld-install-meld-apt

2. Enter your admin password when prompted. When you see the command prompt again, the install is finished and Meld can be opened.

meld-install-meld-password

3. When you open Meld, you’ll see three options: File comparison, Directory comparison and Version control view. Click on “File comparison.”

meld_004

4. Two drop-down menus will appear, both reading “(None).” Click on one to select a file for comparison.

meld_005

5. Click on the other drop-down menu to select a second file for comparison.

meld_020

6. Click the “Compare” button.

meld_006

7. The next window summarizes any differences found between two files. Red characters are differences, and the blue background indicates that there is a difference in that line. A green background indicates that a line exists in one file but not in the other.

meld_007

8. To navigate between differences, click the up and down buttons in the menu bar. You can also click directly in the document with your mouse.

meld_008-2

In addition to making comparisons, Meld allows you to change compared files. Meld will continue to analyze the document for changes as you modify either version, and you can press “Control + Z” at any time to undo changes.

1. To copy changes from one file to another, click the small black arrows between the changes. Clicking the arrow on the left side, for example, will use the left-hand document’s line to overwrite the right-hand document.

meld_009

2. Lines can also be inserted into documents without overwriting. If you hold down the Control key, you’ll see the arrows from the last step turn into diagonal pairs. Click the upward-facing arrow to insert a line above a unique line or the downward-facing arrow to insert a line below.

meld_010

3. Finally, you can type directly into your document. Just insert your cursor as you normally would and begin typing.

4. Once you’re done making changes, click the Save button to save your files.

meld_013

You can use either diff or Meld to compare files in Linux. Meld is a more fully-featured application, but nothing beats the command line for speed.

5 comments

  1. Meld looks perfect for sorting through various text files or scripts!….Gonna install it on my Fedora Linux desktop and see how it works with the Gnome interface! Thanks for the info!!!

  2. Thank you for the article. I had never heard of those programs. Meld is amazing and competent.

  3. I know I can look this up (and will) but I was drawn to this article by it’s title, leaving me a little disappointed that an article titled about ‘diff’ spent waaay more time and space talking about Meld. For instance, you explained the ‘1c1’ notation. How about ‘7,8c7,8’ or other more meaningful examples?

    • Hi George!

      Thanks for the feedback! To explain a little more about how the diff notion works, that “7,8c7,8” means that there’s a difference between the two documents at lines 7 through line 8.

      You’ll also see the notation like 0a1,5, meaning that one file1 (for example) needs to have lines 1 through 5 of file2 appended at line 0 (the start of the document) to make the two documents the same.

      Finally, you’ll see something like 4d8, meaning that file1 needs to have line 4 deleted based on an analysis of line 8 in file2.

      The notation can get a little complex, but it’s easiest to sort out when you’re looking at the two-column view.

      Personally, I like Meld because the GUI makes it easier to sort out where the differences are located visually. And allowing the user to make changes in situ is especially valuable as well.

      Hope that helps expand your understanding of the basics of diff!

      Alexander

Comments are closed.

Sponsored Stories