diff command in linux 1

How to Use diff Command in Linux

The diff Command in Linux is one of those tools that might seem weird for casual users. After all, there are better alternatives that can do the job more efficiently, and visually better when using file explorers.

But for long-time uses, system administrators, and other professionals, diff serves as a handy tool for making a quick comparison of two files. Even better, skilled scripters utilize this command to automate simple tasks that require file comparing.

Combining diff with other commands is another way to maximize its utility. Some commands like ed, ex and patch use diff outputs to produce their outputs. Let’s learn more about this command with the articles and examples below.

What is diff Command?

diff is a file comparison tool that compares files in a line-by-line manner. This tool was created and published way back in 1974 for Unix machines. Most users utilize this tool to compare text files. However, it is also capable of comparing binaries, directories, and even image files.

For Unix-based operations, diff functions with ed to automatically check and modify changes and keep the latest version. In a way, early computer users maximize storage by deleting multiple file versions and keeping the best ones.

But in the current tech landscape, diff is now used in more ways than before. For instance, users can now save the output in a third file for evaluation. The command can also compare directories and enable other commands to operate automatically.

Default diff Command Syntax

The default syntax for using the diff command in Linux is by running diff followed by the first file and the second file. In the output, the < indicates the content of the first file while > shows the second file.

$ diff <file1> <file2>
$diff <option> <file1> <file2>

In the example below, we compared two files named Mumbo and Grian. All the lines with < on it shows the content for Mumbo (file 1) and all the content in > shows text for Grian (file 2). 

diff command in Linux default syntax

If both files have the same content, the prompt won’t show any result. In the example below, there is a difference within the first line of the two documents. As you can see, the diff command shows that Mumbo is a funny guy who has a mustache.

diff command demo

In addition, diff only shows results if it detects any difference within the two files. If the two files have similar content, diff will return zero. By default, it also includes white space and newlines when detecting for changes.

The random numbers and letters above are instructions on how to change the first file to make it identical to the second file. In the above example, 1c1 means changing the first line of the first file (Mumbo) to the first line of the second file (Grian).

Instructions from diff commands are not just a randomly generated string. “c” indicates that there are changes between the two lines. Although c is what you’ll commonly see when using the diff command, there are also other actions such as “a” and “d.”

c – the content was replaced

a – there is content added

d – the content was deleted

Unfortunately, diff can only process two files. For comparing multiple files, you might want to check the diffuse command, which can compare up to 10 files.

Relevant diff Options

Numerous options are available to enhance the functionalities and utility of the diff command in Linux and UNIX systems. If interested, you can check the exhaustive list of options by running diff --help. Here are some of the commonly-used options.

-y: Gives a side-to-side visual comparison of two files in the result.

-s: Signifies that two files are identical.

-r: Use the command recursively, including subdirectories.

-i: Ignore results that only show case-sensitive differences.

-B: Completely disregards empty line differences.

-w: Completely disregards white space differences.

-u: Display the result in an easier to read format.

Example Usages of diff Command

You can use the diff command in three different ways: compare two text files, other files, and directories. Each method can help in multiple situations, depending on the result that you’re trying to achieve.

Using diff Command to Compare 2 Texts

To compare 2 text files, you can use the default syntax and add more options if needed. As always, < pertains to the first file while > points to the second file. Prompts with -B and -w will ignore line or space differences, so add those options if you want a neat-looking and less-cluttered result output.

$ diff file1.txt file2.txt
diff command in Linux example

Using diff Command to Compare 2 Files

Comparing two files will not show the line-by-line, pixel-by-pixel, or other binary details. Although it can still compare files, it will only inform the user if the two files are different.

Just like text files, the diff command in Linux will return zero if the two files are identical. You can use this option if you want to compare two files and automate file transfer without duplicating the data.

$ diff file1.file file2.file
diff command in Linux file sample

Using diff Command to Compare 2 Directories

When comparing two directories, the diff command will return the difference between each folders’ contents. For instance, only the Grian folder below has the Grian.png file, while only the Mumbo folder has the Mumbo.png file. It is a very useful technique if you want to manage multiple versions of the same directory.

$ diff pathname1 pathname2
diff command directory comparison

Saving Results to a File

Do you want to save a diff result for viewing or evaluation later on? You can do that by adding > and the name of the file that you want to save the results into. It’s okay if the results file doesn’t exist yet, it will be created automatically.

$ diff file1 file2 > file3
saving diff result to new file

diff Command with patch

Using diff with patch is useful in cases wherein you need to amend a file from third-party instructions. Instead of manually changing the file line per line, you can use the patch command to automate the amendment.

In the example below, the instruction to change Mumbo.txt to Grian.txt is stored in the Sahara.patch. Once you extract the change instructions, you can use the patch command to apply the changes automatically.

The format for the patch command is:

$patch <target file> <patch file>
diff command in linux 2

Final Thoughts

The diff command in Linux is something that you would either use sparingly or would be a go-to tool depending on the task or the project that you’re dealing with. Hopefully, this article is able to help you learn diff and its various usages. Please share this article and check out other Linux command guides in the How-To section.

If this guide helped you, please share it. ?

Leave a Reply
Related Posts