diff Command in Linux with Examples

This tutorial explains Linux “diff” command, options and its usage with examples.

diff – diff reports the differences between two files.

DESCRIPTION

The diff command compares text files. It can compare single files or the contents of directories.

Note: The diff command only works with input files that are text files.

If the Directory1 and Directory2 parameters are specified, the diff command compares the text files that have the same name in both directories. Binary files that differ, common subdirectories, and files that appear in only one directory are listed.

When the diff command is run on regular files, and when comparing text files that differ during directory comparison, the diff command tells what lines must be changed in the files to make them agree. If neither the File1 nor File2 parameter is a directory, then either may be given as – (minus sign), in which case the standard input is used. If the File1 parameter is a directory, then a file in that directory whose file name is the same as the File2 parameter is used.

advertisement
advertisement

Lines from first file are preceded by a less then symbol ( < ) and lines from second file by a greater then symbol ( > ). A dashed line ( — ) is used to separate output from the two files. The letters can be used to convert file1 into file2:
c   Replace lines from file1 with those from file2.
d   Delete lines from file1.
a   Add lines from file2 to file1.

SYNOPSIS

diff [-b] [-i] [-t] [-w] [-c] [-C] [-e] [-f] [-h] [-n] [-D string] [-l] [-r] [-s] [-S name] [fileone filetwo ] [directoryone directorytwo]

OPTIONS :

-b
Ignores spacing differences.
-i
Ignores case.
-t
Expands TAB characters in output lines. Normal or -c output adds character(s) to the front of each line that may adversely affect the indentation of the original source lines and make the output lines difficult to interpret. This option will preserve the original source’s indentation.
-w
Ignores spaces and tabs.
-c
Produces a listing of differences with three lines of context. With this option output format is modified slightly: output begins with identification of the files involved and their creation dates, then each change is separated by a line with a dozen *’s. The lines removed from file1 are marked with ‘-‘; those added to file2 are marked ‘+’. Lines that are changed from one file to the other are marked in both files with ‘!’.
-C
Produces a listing of differences identical to that produced by -c with number lines of context.
-e
Produces a script of only a, c, and d commands for the editor ed , which will recreate file2 from file1.
-f
Produces a similar script, not useful with ed , in the opposite order.
-h
Does a fast, half-hearted job. It works only when changed stretches are short and well separated, but does work on files of unlimited length. Options -c, -e, -f, and -n are unavailable with -h. diff does not descend into directories with this option.
-n
Produces a script similar to -e, but in the opposite order and with a count of changed lines on each insert or delete command.
-D string
Creates a merged version of file1 and file2 with C preprocessor controls included so that a compilation of the result without defining string is equivalent to compiling file1, while defining string will yield file2.
-l
Produce output in long format. Before the diff, each text file is piped through pr to paginate it. Other differences are remembered and summarized after all text file differences are reported.
-r
Applies diff recursively to common subdirectories encountered.
-s
Reports files that are the identical; these would not otherwise be mentioned.
-S name
Starts a directory diff in the middle, beginning with the file name.
filenameone
File one for comparing.
filenametwo
File two for comparing.
directoryone
Directory one for comparing.
directorytwo
Directory two for comparing.

Note: Join free Sanfoundry classes at Telegram or Youtube

EXAMPLES

Two provide examples for this command, lets consider two files :

$ cat file1.txt
abc
def
ghi
$ cat file2.txt
abc
ghi
klm

1. Simply compare two files

$ diff file1.txt file2.txt
2d1
< def
3a3
> klm

It means one needs to delete 2nd line in first file and add 3rd line from second file at 3rd position in first file to make them same.

advertisement

2. To create a file containing commands that the ed command can use to reconstruct one file from another, enter:

$ diff  -e file1.txt file2.txt > new.to.old.ed

This creates a file named new.to.old.ed that contains the ed subcommands to change file1 into the version of the text found in file2.txt. In most cases, new.to.old.ed is a much smaller file than file2.txt. You can save disk space by deleting file2.txt, and you can reconstruct it at any time by entering:
(cat new.to.old.ed ; echo ‘1,$p’) | ed – file1.txt > file2.txt

The commands in parentheses add 1,$p to the end of the editing commands sent to the ed editor. The 1,$p causes the ed command to write the file to standard output after editing it. This modified command sequence is then piped to the ed command (| ed), and the editor reads it as standard input. The – flag causes the ed command not to display the file size and other extra information because it would be mixed with the text of file2.txt.

advertisement

3. To compare two files while ignoring differences in the amount of white space, enter:

$ diff  -w prog.c.bak prog.c

If two lines differ only in the number of spaces and tabs between words, the diff -w command considers them to be the same.

4. To compare two different directories

$ diff help dir2
Only in help: tab2.gif
Only in help: tab3.gif
Only in help: tab4.gif
Only in help: tape.htm
Only in help: tbernoul.htm
Only in help: tconner.htm
Only in help: tempbus.psd

Compares the directory named help with the directory named dir2.

5. Diff with Context and more verbose Output

You can use the -C n option for n context lines. Output related to file1 is
preceded by stars (***) and file2 by dashes (—). Differences are separated by a long row of stars (***************).
In output the following symbols are used:
! Indicates corresponding lines in the two files that differ.
+ Indicates lines that exist in file2 but not file1.
– Indicates lines that exist in file1 but not file2.

$ diff -c file1.txt file2.txt
*** file1.txt	2013-12-21 09:07:31.493116833 -0800
--- file2.txt	2013-12-21 09:07:46.397117292 -0800
***************
*** 1,3 ****
  abc
- def
  ghi
--- 1,3 ----
  abc
  ghi
+ klm

6. Ignore Case and Repeated Blanks

Report the differences between file1.txt and file2.txt using the -i option to ignore the differences between upper and lower case charactersand the -b option which ignores all repeated blanks and blanks at the end of lines.

$ diff -ib  file1.txt file2.txt

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.