[Chapter 16] 16.23 Comparing Filenames in Two Directory Trees
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:37:59Z

#

| Chapter 16 Where Did I Put That? |

## 16.23 Comparing Filenames in Two Directory Trees

Do you have two directory trees full of subdirectories and files? Would you like to compare the filenames to see if there are some files only in one tree or the other? If you don't have dircmp (16.22), look at the quick-and-dirty substitute in the example below. The numbered prompts (7.2) like `3%` are just for reference:

    [..]
    1% cd directory1
    2% find . -type f -print | sort >/tmp/dir1
    3% cd directory2
    4% find . -type f -print | sort >/tmp/dir2
    5% comm -3 /tmp/dir[12]
    6% rm /tmp/dir[12]

The comm (28.12) command will give you two columns: files in the left-hand column are only in directory1. Files in the right-hand column are only in directory2. You can get other information, too, like a list of files in both trees.

This works nicely for directory trees on other computers, too. Run one `find` `|` `sort` on the remote system. Transfer that file to the computer with the other directory tree and run comm there. Or do the diff across the network by replacing commands 3-5 above with:

    rsh
    % rsh host \
      'cd directory2; find . -type f -print | sort' | \
      comm -e /tmp/dir1 -

The `-` argument tells comm to read its standard input (from the remote find command). Article 13.13 shows a similar trick for a filesystem across a network. Articles 16.19 and 16.20 are about programs that help you see a directory tree.

- JP

16.22 Comparing Two Directory Trees with dircmp |  | 16.24 Counting Files by Types