filebashfindnot-existsdirectory-tree

in bash find all files in flat directory that don't exist in another directory tree


I have many files in a directory A.

Some of those files exist in a directory tree with sub-directories B/B1, B/B2, B/B3, B/B4, ... Note that some files have spaces in their names.

For example:

in directory A:

How can I write a script that will list all the files in A that are not found under the directory tree B?


Solution

  • # A
    # ├── blue file.png
    # └── red file.png
    # B
    # └── small
    #     └── red file.png
    
    $ comm -23 <( find A -type f -printf '%f\n' | sort | uniq ) <( find B -type f -printf '%f\n' | sort | uniq )
    blue file.png
    

    If your find lacks -printf, you can try:

    comm -23 <( find A -type f -exec basename {} \; | sort | uniq ) <( find B -type f -exec basename {} \; | sort | uniq )