【问题标题】:in bash find all files in flat directory that don't exist in another directory tree在 bash 中查找平面目录中不存在于另一个目录树中的所有文件
【发布时间】:2012-07-11 12:35:01
【问题描述】:

我在一个目录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 下找不到的所有文件?

【问题讨论】:

    标签: file bash find not-exists directory-tree


    【解决方案1】:

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

    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'
    

    【讨论】:

      【解决方案2】:
      # 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
      

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

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

      【讨论】:

      • 谢谢。我的“查找”似乎没有 -printf,所以我改用了 -print。这应该没什么区别吧?
      • 等一下。有些东西不工作。我仍然在 A 和 B 中都存在的结果中获取文件。
      • 好的。也许是-printf。当我改用 -print 时,我得到了整个文件名,包括目录。如果 -printf '%f' 给了我文件的基本名称,我会接受答案,但是我如何使这个脚本适应没有 -printf 的“查找”?
      • 试试:comm -23 &lt;( find A -type f -exec basename {} \; | sort | uniq ) &lt;( find B -type f -exec basename {} \; | sort | uniq )
      • 您使用的是什么操作系统/版本?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 2018-04-01
      • 2015-12-06
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 2012-03-11
      相关资源
      最近更新 更多