Next: , Previous: , Up: Comparing and Merging Files   [Contents][Index]

4 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 --report-identical-files (-s) 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.

If only one file exists, diff normally does not show its contents; it merely reports that one file exists but the other does not. You can make diff act as though the missing file is empty, 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 the missing file is in the first or the second position.) To do this, use the --new-file (-N) option. This option affects command-line arguments as well as files found via directory traversal; for example, ‘diff -N a b’ treats a as empty if a does not exist but b does.

If the older directory contains 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 inserts the contents only 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 and Using Patches, for more discussion of making patches for distribution.

To ignore some files while comparing directories, use the --exclude=pattern (-x 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 --exclude-from=file (-X file) option. Trailing white space and empty lines are ignored in the pattern file.

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 --starting-file=file (-S 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 --exclude=pattern (-x pattern) option, or an --exclude-from=file (-X 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.

To tell diff not to follow a symbolic link, use the --no-dereference option.


Next: , Previous: , Up: Comparing and Merging Files   [Contents][Index]