在 bash 中查找平面目录中不存在的其他目录树中的所有文件



目录A中有许多文件。

其中一些文件存在于具有子目录B/B1B/B2B/B3B/B4…的目录树中。。。请注意,有些文件的名称中有空格。

例如:

在目录A:中

  • 有一个名为A/red file.png 的文件

  • 还有一个叫A/blue file.png

    以及,在目录树B:中

  • 有一个名为B/small/red file.png 的文件

    在本例中,我想要一个脚本来告诉我文件blue file.png不存在于目录B中。

如何编写一个脚本,列出A中目录树B下找不到的所有文件?

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

如果您的find缺少-printf,您可以尝试:

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

这个版本可以处理所有文件名,包括包含换行符的文件名:

comm -z23 <(find dir1 -type f -printf '%f' | sort -uz) <(find dir2 -type f -printf '%f' | sort -uz) | xargs -0 printf '%sn'

最新更新