Diff is a command line tool for Unix systems that allows you to compare two files or directories and see the differences between them. This is ideal for comparing old and new versions of files to see what has changed. In this article, we show you how you can use diff
To easily compare files in Linux.
advice: Learn how to copy and paste text, files and folders in the Linux terminal.
Comparing files with diff
- Open a terminal window.
- Type
diff
and the paths to two files you want to compare. In this case, I’m comparing two small C programs on my desktop using the following command:
diff ~/Desktop/hello.c ~/Desktop/hello-1.c
- to press to enter Submit the command. The resulting view will show you all the different lines between the files. The row numbers compared will be displayed as “1c1”, meaning the first row has been changed, and the following differences have been found.
![diff-terminal-2 A terminal window showing the output of the diff command.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-03-sample-diff-output.png)
- To see a side-by-side view of the files and their differences, use
-y
flag afterdiff
. for example:
diff -y ~/Desktop/hello.c ~/Desktop/hello-1.c
![diff-terminal-3 A terminal window showing a command that will print a visual diff.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-04-visual-diff-command-flag.png)
Each line with a pipe symbol ( |
) is a line with difference. Just be sure to maximize your terminal window first, or you may have trouble seeing the display.
![diff-terminal-4 A terminal window showing the output of the visual diff command.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-05-sample-visual-diff-output.png)
good to know: Learn how to manipulate the output of a program by using Bash pipes along with the powerful Sed utility.
Compare files with Meld
The functionality of diff
can also be accessed via MeldUser interface for diff
. Given that diff’s user interface can be difficult to get hold of, Meld is a useful tool.
- Install Meld from the command line by opening a terminal and typing:
or
For RPM based distributions.
This will launch the apt package manager and start installing Meld. Note that the -y
The flag will automatically say “yes” to any prompt; Please remove it if you prefer to confirm these instructions manually.
![meld-install-meld-apt A terminal window showing the installation command for Meld.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-06-apt-command-install-meld.png)
- Enter your administrator password when prompted. When you see the command line again, the installation is complete and Meld can be opened.
![meld-install-meld-password A terminal window showing the Meld installation process.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-07-installing-meld.png)
- When you open Meld, you’ll see three options: Compare Files, Compare Directories, and View Version Control. Click “File”.
![meld_004 Screenshot of the Meld splash screen.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-08-select-file-comparison-option-meld.png)
- Two drop-down menus will appear, both reading “(None).” Click one to select a file to compare.
![meld_005 A screenshot of the Meld screen with the first file selector highlighted.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-09-select-first-file-comparison-meld.png)
- Click the second drop-down menu to select a second file to compare.
![meld_020 A screenshot of the Meld screen with the second file selector highlighted.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-10-select-second-file-comparison-meld.png)
- Click the “Compare” button.
![meld_006 A screenshot of the Meld screen with an emphasis on the compare button.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-11-select-compare-button-meld.png)
- The next window summarizes the 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 A screenshot of the Meld program with a simple comparison view of two files.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-12-sample-meld-interface.png)
- To navigate between diffs, press the up and down buttons on the menu bar. You can also click directly in the document with the mouse.
![meld_008-2 A screenshot of the Meld program highlighting its difference navigation button.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-13-select-diff-movement-meld.png)
good to know: If you prefer to use online tools to compare files, these are some of the best online tools for checking differences.
File manipulation in Meld
In addition to performing comparisons, Meld allows you to modify files in comparison. Meld will continue to analyze the document for changes while changing one of the versions, and you can click Ctrl + G at any time to cancel the changes.
- To copy changes from one file to another, click the small black arrows between the changes. Clicking the arrow on the left, for example, will use a row of the left document to replace the document on the right.
![meld_009 A screenshot of the Meld program showing the resolve difference buttons.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-14-select-changes-diff-meld.png)
- Lines can also be inserted into documents without replacement. if you hold the Ctrl key, you’ll see the arrows from the last step turn into plus signs. Click the icon to open a small dialog where you can choose where to add a new row.
![meld_010 A screenshot of the Meld program showing the request to add its Diff.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-15-insert-unique-line-comparison-meld.png)
- Finally, type directly into your document. Just insert the cursor as usual and start typing.
- Once you’re done making changes, click the “Save” button to save your files.
![meld_013 A screenshot of the Meld program highlighting its save button for each file.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-16-save-file-comparison-meld.png)
Creating diff files using Meld and diff
Besides showing the differences between two files, you can also use Meld to create “Miff Files”. These are small pieces of text that contain instructions on how to apply changes from one file version to another. This is useful if you want to quickly distribute code updates without re-sharing the entire source archive.
- Open the two files you want to compare.
![A screenshot of the Meld program showing a simple difference.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-17-open-sample-diff-file-meld.png)
- Click the options menu in the upper left corner of the window.
![A screenshot highlighting the Meld options button.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-18-select-options-menu-meld.png)
- Select the “Compare” sub-menu.
![A screenshot of the Meld options menu with the Comparison sub-menu highlighted.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-19-select-comparison-option-meld.png)
- Click on the “Format as patch” sub-option.
![A screenshot of the Comparison submenu with an emphasis on the Design submenu.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-20-select-format-patch-meld.png)
- This will open a small window where Meld will print the entire difference file between your two files. At this point, you can copy this text to your clipboard or save it to a file. To do this, click the “Save Patch” button.
![A screenshot of the Meld's Patch subwindow with emphasis on "Save a patch" button.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-21-select-save-patch-meld.png)
While creating diff files within Meld is simple and straightforward, there are times when the GUI may not be accessible while editing code. For this, you can also use diff
A program to create a diff file.
- Open a terminal and check if there are any differences between the two files:
![Terminal showing a simple difference between two files.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-22-check-diff-status.png)
- Create a simple diff file using the
-c
flag:
diff -c hello.c hello-1.c > hello.diff
![Terminal showing basic diff file output.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-23-create-simple-diff-file.png)
- Alternatively, use
-u
An option that combines the two text streams into one unified difference block. This can be helpful if you modify a lot of code and want to save disk space:
diff -u hello.c hello-1.c > hello.diff
![A terminal showing a more complex unified dipper output.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-24-create-unified-diff-file.png)
Using diff files to modify source code
As discussed above, a diff file allows you to create an easily shareable summary of changes to your source code. However, diff itself cannot apply these to existing files. To do this, use the diff companion program, patch
.
- Determine the original source code that the diff is trying to modify:
![Terminal showing the title of the original source file.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-25-determine-original-file-diff-file.png)
- Create a backup of your original source code:
- Run the patch program with the original file as the first argument and the diff file as the second:
![A terminal that displays a simple patch command for a single source file.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-26-simple-patch-diff-file.png)
It is important to note that a diff file can also contain changes that span multiple files. Apply it by placing all the files needed for the update in a single directory and run it patch < multi-file.diff
.
- Check if the repair program has successfully updated your source file by comparing it to your backup:
diff -y hello.c hello.c.old
![Terminal showing the visual difference between the new source file and the original file.](https://www.maketecheasier.com/assets/uploads/2023/05/use-diff-files-linux-27-check-diff-result.png)
for your information: Git is a powerful cross-platform version control software. Learn how you can use it to manage your Linux source code repositories.
Common questions
Is it possible to repair a file multiple times?
Yes. However, it is important to note that the line number value within a diff file is static. For example, applying a diff that adds five lines of code at line 1 will align the entire file by five lines. While modern tools can compensate for changes from single differences, implementing more than one can create unnecessary code conflicts.
Is a diff file similar to a Git patch?
Yes. Both diff and Git use a similar type of format for their code revisions. As a result, you can switch between the two utilities when applying updates to your source code. For example running git apply hello.diff
Worth a run patch < hello.diff
.
Can you use Meld with version control software like Git?
Yes. One of Meld’s strongest features is that it can work transparently with any version control software. This gives you the ability to easily interact with your repositories outside of the terminal interface.
To do this, tap Ctrl + N, and then click the “View Version Control” button. Then, click the drop-down box below it and select “Other.” This will bring up a small window where you can view and load your Git repository.
Image credit: Unsplash. All screenshots by Ramces Red.
Sign up for our newsletter!
Our latest tutorials are delivered straight to your inbox