目录A
中有许多文件。
其中一些文件存在于具有子目录B/B1
、B/B2
、B/B3
、B/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'