Basic UNIX Commands and Beginner Tutorials

5 UNIX diff Command Examples of How to Compare Two Text Files


The UNIX diff command compares the contents of two text files and outputs a list of differences. If desired, you may instruct it to ignore spacing or case variations. This command can also verify that two files contain the same data. The syntax is relatively simple:

diff [options] file1 file2
When this UNIX file comparison tool finds differences, it lists all of the lines that do NOT match. A greater-than or less-than symbol appears at the beginning of each line. "<" means that the text appears in file1, and ">" indicates that it comes from file2. This UNIX diff command example shows how it works with no options:

# diff file1 file2
1,5c1,5
< this is line 1 The UNIX diff command is used to compare (find the differences) between two files.
< this is line 2 This line demonstrates how the diff command handles white space
< this is line 3 if ( a == b )
< this is line 4 THE DIFF COMMAND IS HELPFUL WHEN COMPARING SOURCE CODE FILES
< this is line 5
---
> this is line 1
> this is line 2 This line demonstrates how the diff command handles white     space
> this is line 3 if(a==b)
> this is line 4 The diff Command is Helpful When Comparing Source Code Files
> this is line 5 The UNIX diff command is used to compare (find the differences) between two files.
Before listing lines of text, this tool shows you how to eliminate all of the differences. It supplies Ed line editor commands, such as "1,5c1,5." This means that you could make the files match by modifying lines one through five. The letter "c" stands for "change." Diff's directions may also contain "a" for "append" or "d" for "delete."



For example, you would have to change lines two through four if the command outputs "2,4c2,4." The file difference tool will use one number with no comma if a single line is affected. You may convert file1 into file2 by following the instructions as they appear. To make file2 match file1, follow them in reverse and swap "d" for "a."

When you compare two text files, it's possible that diff will not find any differences. The output is completely blank if this happens. Diff also behaves differently when you enter directory names. It will compare file2 with an item in directory1 that has the same name. If you specify two directory names, it checks files in both directories that have matching names.

Although people typically use diff to compare text files, you may also check a document against standard input. You can try this by replacing file1 or file2 with a hyphen ("-"). Diff will use words you type at the keyboard to perform the comparison. This function may be helpful if you need to compare written text with a file. Furthermore, diff can use stdin to receive output data that you pipe in from another UNIX command. It will also accept two hyphens, but there is little reason to compare stdin to itself.

This command has a variety of helpful options. If you use the "-b" setting, it skips over minor spacing differences. This UNIX diff example shows that "-b" instructs the system to ignore extra spaces and tabs:

# diff -b file1 file2
1c1
< this is line 1 The UNIX diff command is used to compare (find the differences) between two files.
---
> this is line 1
3,5c3,5
< this is line 3 if ( a == b )
< this is line 4 THE DIFF COMMAND IS HELPFUL WHEN COMPARING SOURCE CODE FILES
< this is line 5
---
> this is line 3 if(a==b)
> this is line 4 The diff Command is Helpful When Comparing Source Code Files
> this is line 5 The UNIX diff command is used to compare (find the differences) between two files.
The "-b" option proves useful if you want to compare formatted and unformatted files. Alternately, you may ignore all spacing variations with the stronger "-w" setting:

# diff -w file1 file2
1c1
< this is line 1 The UNIX diff command is used to compare (find the differences) between two files.
---
> this is line 1
4,5c4,5
< this is line 4 THE DIFF COMMAND IS HELPFUL WHEN COMPARING SOURCE CODE FILES
< this is line 5
---
> this is line 4 The diff Command is Helpful When Comparing Source Code Files
> this is line 5 The UNIX diff command is used to compare (find the differences) between two files.
To disregard case differences, add the "-i" option. You can use it to check for mistakes after converting an uppercase document to mixed-case characters. It works the same way as it does with the UNIX grep command:

# diff -i file1 file2
1,3c1,3
< this is line 1 The UNIX diff command is used to compare (find the differences) between two files.
< this is line 2 This line demonstrates how the diff command handles white space
< this is line 3 if ( a == b )
---
> this is line 1
> this is line 2 This line demonstrates how the diff command handles white     space
> this is line 3 if(a==b)
5c5
< this is line 5
---
> this is line 5 The UNIX diff command is used to compare (find the differences) between two files.



The "-c" option is handy for comparing two program code revisions. It reveals the context of every difference by listing six adjacent lines and putting an exclamation point before the line that differs. A plus or minus sign appears when text has been added or deleted from file2. Furthermore, it lists the times and dates when both files were updated:

# diff -c file1 file2
*** file1 Thu Jan 23 13:36:42 2014
--- file2 Thu Jan 23 13:37:13 2014
***************
*** 1,5 ****
! this is line 1 The UNIX diff command is used to compare (find the differences) between two files.
! this is line 2 This line demonstrates how the diff command handles white space
! this is line 3 if ( a == b )
! this is line 4 THE DIFF COMMAND IS HELPFUL WHEN COMPARING SOURCE CODE FILES
! this is line 5
--- 1,5 ----
! this is line 1
! this is line 2 This line demonstrates how the diff command handles white     space
! this is line 3 if(a==b)
! this is line 4 The diff Command is Helpful When Comparing Source Code Files
! this is line 5 The UNIX diff command is used to compare (find the differences) between two files.
You may combine multiple options and use a single hyphen. For instance, "-iw" ignores white space and case differences. Plentiful options have helped to establish diff as a flexible UNIX file comparison tool that simplifies programming, editing and data management tasks.

Do you want to learn more UNIX or Linux commands, or how to read and write shell scripts? If you are ready to move past the basics, either of these online courses is a good place to start...

UNIX and Linux Operating System Fundamentals contains a very good "Introduction to UNIX Shell Scripting" module, and should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts.

UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.

Both courses include access to an Internet Lab system for completing the course's hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist.