diff - Unix, Linux Command


Advertisements

Previous Page
Next Page  
 

NAME

diff -compare files line by line

SYNOPSIS

  diff [OPTION]... FILES

DESCRIPTION

Compare FILES line by line.

OPTIONS

TAG DESCRIPTION
--normal output a normal diff (the default)
-q, --brief report only when files differ

 

-s, --report-identical-files report when two files are the same
-c, -C NUM, --context[=NUM] output NUM (default 3) lines of copied context
-u, -U NUM, --unified[=NUM] output NUM (default 3) lines of unified context
-e, --ed output an ed script

 

-n, --rcs output an RCS format diff.
-W, --width=NUM ououtput at most NUM (default 130) print columnst

 

-y, --side-by-side output in two columns

 

--left-column output only the left column of common lines

 

--suppress-common-lines do not output common lines

 

-p, --show-c-function show which C function each change is in

 

-F, --show-function-line=RE show the most recent line matching RE

 

--label LABEL use LABEL instead of file name and timestamp (can be repeated)

 

-t, --expand-tabs expand tabs to spaces in output

 

-T, --initial-tab make tabs line up by prepending a tab

 

--tabsize=NUM tab stops every NUM (default 8) print columns

 

--suppress-blank-empty suppress space or tab before empty output lines

 

-l, --paginate pass output through 'pr' to paginate it

 

-r, --recursive recursively compare any subdirectories found

 

N, --new-file treat absent files as empty

 

--unidirectional-new-file treat absent first files as empty

 

--ignore-file-name-case ignore case when comparing file names

 

--no-ignore-file-name-case consider case when comparing file names

 

-x, --exclude=PAT exclude files that match PAT

 

-X, --exclude-from=FILE exclude files that match any pattern in FILE

 

-S, --starting-file=FILEd start with FILE when comparing directories

 

--from-file=FILE1 compare FILE1 to all operands; FILE1 can be a directory

 

--to-file=FILE2 compare all operands to FILE2; FILE2 can be a directory

 

-i, --ignore-case ignore case differences in file contents

 

-E, --ignore-tab-expansion ignore changes due to tab expansion

 

-Z, --ignore-trailing-space ignore white space at line end

 

-b, --ignore-space-change ignore changes in the amount of white space

 

-w, --ignore-all-space ignore all white space

 

-B, --ignore-blank-lines ignore changes where lines are all blank

 

-I, --ignore-matching-lines=RE ignore changes where all lines match RE

 

-a, --text treat all files as text

 

--strip-trailing-cr strip trailing carriage return on input

 

D, --ifdef=NAME output merged file with '#ifdef NAME' diffs

 

--GTYPE-group-format=GFMT format GTYPE input groups with GFMT

 

--line-format=LFMT format all input lines with LFMT

 

--LTYPE-line-format=LFMT format LTYPE input lines with LFMT

 

EXAMPLES

EXAMPLE-1:

Lets use these two files: file1.txt and file2.txt:

If file1.txt contains the following four lines of text:
I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed.

...and file2.txt contains these four lines:

I need to buy apples.
I need to do the laundry.
I need to wash the car.
I need to get the dog detailed.

...then we can use diff to automatically display for us which lines differ between the two files with this command:

$ diff file1.txt file2.txt
		output:
		2,4c2,4
		< I need to run the laundry.
		< I need to wash the dog.
		< I need to get the car detailed.
		---
		> I need to do the laundry.
		> I need to wash the car.
		> I need to get the dog detailed.

EXAMPLE-2:

To view differences in context mode, use the -c option:

$ diff -c file3.txt file4.txt
		output:
		*** file3.txt   2014-08-21 17:58:29.764656635 -0400
		--- file4.txt   2014-08-21 17:58:50.768989841 -0400
		***************
		*** 1,4 ****
		  apples
		- oranges
		  kiwis
		  carrots
		--- 1,4 ----
		  apples
		  kiwis
		  carrots
		+ grapefruits


EXAMPLE-3:

To perform diff between all types of files using -a"

$ diff -a mine.txt parent.txt a.out

output: ==== 1:1,2c Hello This is file 2:1,3c Hello This is file c ya 3:1,4c ELF>@@@h@8 @@@@@@��88@8@@@�� ` @@`@`��TT@T@DDP�td@@@@@$$Q�tdR�td``��/lib64/ld-linux-x86-64.so.2GNUGNUKd�3�K!���� ��3�٥� __gmon_start__libc.so.6put



EXAMPLE-4:

To Merge output using -m option:
A merged file comprising of contents from all the three files can be produced using the -m option.

For example, if following are the three files:

$ cat mine.txt
Hi,
This is your file.
Hello

$ cat parent.txt
Hi,
This is your file.

$ cat your.txt
Hi,
This is your file.

Now if diff is run using the -m option :

$ diff -m mine.txt parent.txt your.txt

output: Hi, This is your file. Hello

So the above output shows contents of merged file that has merged contents from all the three files supplied as arguments to diff.


Previous Page
Next Page