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.
Comparing files with diff
1. Open a terminal window.
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 ~/Desktop/python-guessing-game-1.py ~/Desktop/python-guessing-game-2.py
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.
4. To see a side-by-side view of the files and their differences, use the
-y flag after
diff. For example:
diff -y ~/Desktop/python-guessing-game-1.py ~/Desktop/python-guessing-game-2.py
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.
Comparing files with Meld
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:
sudo apt-get -y install meld
sudo yum install meld
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.
2. Enter your admin password when prompted. When you see the command prompt again, the install is finished and Meld can be opened.
3. When you open Meld, you’ll see three options: File comparison, Directory comparison and Version control view. Click on “File comparison.”
4. Two drop-down menus will appear, both reading “(None).” Click on one to select a file for comparison.
5. Click on the other drop-down menu to select a second file for comparison.
6. Click the “Compare” button.
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.
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.
Manipulating files in Meld
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.
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.
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.
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.