Node:Comparing Directories, Next:, Previous:Incomplete Lines, Up:Top



Comparing Directories

You can use diff to compare some or all of the files in two directory trees. When both file name arguments to diff are directories, it compares each file that is contained in both directories, examining file names in alphabetical order as specified by the LC_COLLATE locale category. Normally diff is silent about pairs of files that contain no differences, but if you use the -s or --report-identical-files option, it reports pairs of identical files. Normally diff reports subdirectories common to both directories without comparing subdirectories' files, but if you use the -r or --recursive option, it compares every corresponding pair of files in the directory trees, as many levels deep as they go.

For file names that are in only one of the directories, diff normally does not show the contents of the file that exists; it reports only that the file exists in that directory and not in the other. You can make diff act as though the file existed but was empty in the other directory, so that it outputs the entire contents of the file that actually exists. (It is output as either an insertion or a deletion, depending on whether it is in the first or the second directory given.) To do this, use the -N or --new-file option.

If the older directory contains one or more large files that are not in the newer directory, you can make the patch smaller by using the --unidirectional-new-file option instead of -N. This option is like -N except that it only inserts the contents of files that appear in the second directory but not the first (that is, files that were added). At the top of the patch, write instructions for the user applying the patch to remove the files that were deleted before applying the patch. See Making Patches, for more discussion of making patches for distribution.

To ignore some files while comparing directories, use the -x pattern or --exclude=pattern option. This option ignores any files or subdirectories whose base names match the shell pattern pattern. Unlike in the shell, a period at the start of the base of a file name matches a wildcard at the start of a pattern. You should enclose pattern in quotes so that the shell does not expand it. For example, the option -x '*.[ao]' ignores any file whose name ends with .a or .o.

This option accumulates if you specify it more than once. For example, using the options -x 'RCS' -x '*,v' ignores any file or subdirectory whose base name is RCS or ends with ,v.

If you need to give this option many times, you can instead put the patterns in a file, one pattern per line, and use the -X file or --exclude-from=file option.

If you have been comparing two directories and stopped partway through, later you might want to continue where you left off. You can do this by using the -S file or --starting-file=file option. This compares only the file file and all alphabetically later files in the topmost directory level.

If two directories differ only in that file names are lower case in one directory and upper case in the upper, diff normally reports many differences because it compares file names in a case sensitive way. With the --ignore-file-name-case option, diff ignores case differences in file names, so that for example the contents of the file Tao in one directory are compared to the contents of the file TAO in the other. The --no-ignore-file-name-case option cancels the effect of the --ignore-file-name-case option, reverting to the default behavior.

If an -x pattern, --exclude=pattern, -X file, or --exclude-from=file option is specified while the --ignore-file-name-case option is in effect, case is ignored when excluding file names matching the specified patterns.