Contents|Index|Previous|Next

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, ex-amining file names in alphabetical order. 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 '-P' or
'--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 Tips for Making Patch Distributions 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 subdi-rectories 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, using 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 subsequent files in the topmost directory level.