0

I have a directory (Directory A) with files.

Directories B and C contain files from directory A (no files in B are in C and vice versa).

How can I list files in Directory A that are not present in Directories B nor C?

Robco
  • 17
  • 5

1 Answers1

3

If you don't need to be fast in very-high-volume cases:

for f_path in a/*; do f=${f_path#a/}
  [[ -e "b/$f" || -e "c/$f" ]] && continue
  printf '%s\n' "$f"
done

If you do, and have GNU comm, find and sort, see the following -- of course, replace the tr at the end with code that actually reads a NUL-delimited list properly if you want to be able to safely handle all possible filenames:

comm -z23 <(find a -maxdepth 1 -printf '%P\0' | sort -z) \
          <(find b c -maxdepth 1 -printf '%P\0' | sort -z) \
  | tr '\0' '\n'

For more on this use of comm, see BashFAQ #36.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    With the first solution (globbing), use `shopt -s nullglob` to handle an empty `a` directory correctly. Also, use `shopt -s dotglob` if the files to be checked may have names that begin with a dot. – pjh Sep 05 '18 at 16:14