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

不打扰是莪最后的温柔 提交于 2019-12-10 10:20:53

问题


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:

  • there's a file named A/red file.png
  • there's another named A/blue file.png

    and, in directory tree B:

  • there's a file named B/small/red file.png

    In this example, I would like a script to tell me that the file blue file.png does not exist in the directory B.

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


回答1:


# 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 )



回答2:


This is version which can cope with all filenames, including ones containing newlines:

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


来源:https://stackoverflow.com/questions/11440332/in-bash-find-all-files-in-flat-directory-that-dont-exist-in-another-directory-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!