diff

find differences between two files

Syntax

diff [options] from-file to-file

Description

In the simplest case, diff compares the contents of the two files from-file and to-file. A file name of - stands for text read from the standard input. As a special case, diff - - compares a copy of standard input to itself. If from-file is a directory and to-file is not, diff compares the file in from-file whose file name is that of to-file, and vice versa. The non-directory file must not be -.

If both from-file and to-file are directories, diff compares corresponding files in both directories, in alphabetical order; this comparison is not recursive unless the -r or --recursive option is given. diff never compares the actual contents of a directory as if it were a file. The file that is fully specified may not be standard input, because standard input is nameless and the notion of ‘‘file with the same name’’ does not apply.

diff options begin with -, so normally from-file and to-file may not begin with -. However, -- as an argument by itself treats the remaining arguments as file names even if they begin with -.

Options

-a     Treat all files as text and compare them line-by-line, even if they do not seem to be text.

-b     Ignore changes in amount of white space.

-B     Ignore changes that just insert or delete blank lines.

-F regexp
       In context and unified format, for each hunk of differences, show some of the last preceding line that matches regexp.

-i     Ignore changes in case; consider upper- and lower-case letters equivalent.

-I regexp
       Ignore changes that just insert or delete lines that match regexp.

-r     When comparing directories, recursively compare any subdirectories found.

--recursive
       When comparing directories, recursively compare any subdirectories found.

Example 1

Compare two files with diff command.

[eleiss@eleiss ~]$ cat hello-1.c 
#include<stdio.h>

int main()
{
	printf("Hello");
	return 0;
}
[eleiss@eleiss ~]$ cat hello-1.1.c 
#include<stdio.h>

int main()
{
	printf("Hello World");
	printf("This is the updated version of this program");
	return 0;
}
[eleiss@eleiss ~]$ diff hello-1.c hello-1.1.c 
5c5,6
< 	printf("Hello");
---
> 	printf("Hello World");
> 	printf("This is the updated version of this program");
[eleiss@eleiss ~]$

In this example, we have compared two program files. The second file is the upgraded version of the first program file.

By looking at the output we can say, "hello-1.c" needs to change its line number 5. The output contains the line number 5. In addition it also needs to add one more line at line number 6.


You may write to resources@eleiss.com for any Linux related queries.

advertisement