Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

diff - compare two files

&pagelevel(4)&pagelevel

The diff utility will compare the contents of file1 and file2 and write to standard output a list of changes (with ed-like commands) necessary to convert file1 into file2. This list should be minimal. No output will be produced if the files are identical.


Syntax


diff[ option] file1 file2

No option specified

If the compared files are identical, diff produces no output. When the files differ, the output is as follows:


1.line[range] from file1   ed command  line[range] from file2
2.lines that are only in file1
3.lines that are only in file2


The lines are displayed in the following format:


             a
1.  n1[,n2]  d  n1[,n2]
             c
2.  < text of line from file1
         .
         .
         .
         - - -
3.  > text of line from file2
         .
         .
         .


a, d, and c are ed-like commands meaning:


aappend
ddelete
cchange


The output is to be interpreted as follows:

The ed commands a, d, and c with their preceding line (or range) specifications show how file1 can be converted into file2.

If you replace a with d and d with a and use the line (or range) specifications to the right, the commands indicate how file2 can be converted into file1. Lines from file1 are marked <; those from file2 are marked >.


option

-a

diff produces a diff with all lines of context from both files. Those lines only in file1 are prepended with -; those only in file2 are prepended with +. Lines which are identical in both files are prepended with ' '.

-b

diff ignores trailing blanks or tabs at the end of lines as well as differing numbers of blanks at corresponding positions within text lines. Leading blanks and blank lines are reported as differences.

-i

diff ignores the case of letters; for example, it considers ’A’ to be the same as ’a’.

-t

diff expands tab characters in output lines. In normal or -c output, diff adds character(s) to the start of each line that may adversely affect the indentation of the original source lines and make the output lines difficult to interpret. The -t option preserves the original source’s indentation.

-w

diff ignores all blanks (space and tab characters) and treats strings of blanks of any length as equivalent; for example, it considers 'if ( a = = b )' to be the same as 'if(a==b)'.


The following options are mutually exclusive:

-c

diff produces a listing in three parts with a slightly modified output format.

The output begins with the names and creation dates of file1 and file2. Then come the lines that differ, with the lines not present in file2 marked with a minus sign (-) and the lines that differ in file1 and file2 marked with an exclamation point (!). There are also three lines of context before and after the differing lines.

-C number

Produces a listing of differences identical to that produced by -c, but with number lines of context before and after each difference.

-e

The -e option must not be combined with -l or -s.

diff produces output suitable for use as an ed script. The ed script contains a, d, and c commands and the related text lines as input for the editor ed to convert file1 to file2. Before you pass the script to the editor, you should add the statements w and q at the end of the script and insert the command e file1 at the start (see ed).

-f

diff generates a similar script to the one created with option -e, but converting in the opposite direction. This script, however, is not usable with ed.

Editing scripts produced under the -e and -f options may be incorrect when dealing with lines comprising nothing but a single period.


The following options modify the way in which diff works:

-n

Produces a script similar to -e, but with the ed commands listed in reverse order. In addition, a count of the lines to be changed is printed after each insert or delete command.

-D string

In this case file1 and file2 must be C source programs or contain C source fragments. diff creates a merged version of file1 and file2 with C preprocessor controls included so that compilation of the result is equivalent to compiling file1 if string is not defined, and is equivalent to compiling file2 if string is defined.


The following options are used to compare directories:

-l

diff produces output in long format. Before checking for differences, diff pipes each text file through pr to paginate it. Differences other than those in text files are remembered and are summarized after all text file differences have been reported.

-r

Applies diff recursively to all common subdirectories encountered.

-s

Reports files that are identical; these would not otherwise be mentioned.

-S name

Starts a directory diff in the middle, beginning with the file name.


file1 file2

Names of the files diff is to compare. If file1 is a directory, then a file in that directory with the same name as file2 is compared against file2. If file2 is a directory, then the comparison is made with a file named file1 from that directory.

If both file1 and file2 are directories, diff looks in both directories for files with the same name to compare. In this case diff will not compare block special files, character special files or FIFO special files to any files as well as regular files to directories.

If either the file1 or file2 operand is a -, the standard input will be used in its place. In this case the input files must be text files.

Exit status

0Files are the same
1Files are different
>1Input error

Error

diff: two filename arguments required

You have specified an incorrect number of files. Only two files can be compared at a time.


diff: No such file or directory

One of the specified files does not exist.


diff: Permission denied

You do not have read permission for one of the specified files.

Variable

TZ

Determine the locale for affecting the timezone used for calculationg file timestamps written with the -C and -c options.

Locale

The following environment variables affect the execution of diff:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files).

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

LC_TIME

Determine the locale for affecting the format of timestamps written with the -C and -c options.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example 1

The files file1 and file2 have the following contents:

file1                file2
Jack Jill            Jack and Jill
went up the hill     went uphill
to fetch a pail      of water
of water


By calling diff, you can identify the lines in which the two files differ.

$ diff file1 file2
1,3c1,2
<Jack Jill
<went up the hill
<to fetch a pail
- - -
>Jack and Jill
>went uphill


Explanation: In order to convert file1 into file2, lines 1 to 3 inclusive from file1 (1,3) must be replaced (c) by lines 1 to 2 inclusive (1,2) of file2. The contents of each of these lines are shown in the output lines beginning with the < or > characters. Lines in file1 are marked <; lines in file2 are marked >.

Example 2

Compare files and produce an ed script:

Contents of file1:      Contents of file2:
today is monday         today is tuesday
it is cold              it is autumn 
                           it is cold


After the following command, diff outputs the ed commands with which ed could convert file1 into file2. To be able to use this output as input for ed, you need to add the statements w and q to the end of the ed script.

$ diff -e file1 file2
1c
today is tuesday
it is autumn
.
$

See also

cmp, comm, ed, pr