分别在每个文件夹分支中查找第一个匹配项

给定以下文件夹布局:

Current_folder
└── FolderA
|   └── CMakeList.txt
|   └── FolderAA
|       └── CMakeList.txt
|
└── FolderB
|   └── FolderBA
|   |   └── CMakeList.txt
|   |   └── FolderBAA
|   |       └── CMakeList.txt
|   |
|   └── FolderBB
|       └── CMakeList.txt
|
└── FolderC
    └── CMakeList.txt
    └── FolderCA
        └── CMakeList.txt

如何使用find(或任何其他工具)产生类似于以下内容的输出:

Current_folder/FolderA/CMakeList.txt
Current_folder/FolderB/FolderBA/CMakeList.txt
Current_folder/FolderB/FolderBB/CMakeList.txt
Current_folder/FolderC/CMakeList.txt

这意味着在分别与每个递归文件夹分支的文字“ CMakeLists.txt”匹配之后,搜索将停止。

对我有用的是以下bash脚本:

#!/bin/bash
file_of_interest="CMakeList.txt"
latest_directory_wih_match=""

for current_directory in $(find -type d);
do
  if [[ (${latest_directory_wih_match} == "") || (! ${current_directory} == ${latest_directory_wih_match}*) ]];
  then
    if [ -f ${current_directory}/${file_of_interest} ]
    then
      latest_directory_wih_match=${current_directory}
      echo ${latest_directory_wih_match}/${file_of_interest}
    fi
  fi;
done

@dessert的答案也适用于上面的最小示例。在我的实际文件夹结构(〜4000个文件夹)上使用它,仍然给出错误的结果。老实说,我不确定为什么。

当我尝试@muru的解决方案时,它不会停止执行。我猜函数调用本身没有正确中止吗?也不知道这里的原因。

PS:在实际的CMake项目中,该文件实际上称为“ CMakeLists.txt”(请注意最后的s)。我仅在提供答案后才发现此错误。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Multiindex数据框中查找每个索引的第一个匹配项

返回Powershell中的第一个匹配文件夹

使用grep仅获取每个文件中的第一个匹配项

查找文件中第一个匹配项的快捷方式

如何在多个文件中查找第一个匹配项

播放随机文件夹/子文件夹中的第一个视频

将第一个文件从每个文件夹复制到Windows中的新位置

将每个 Excel 文件分别压缩到一个文件夹中。

取得一个正则表达式文件,然后在另一个文件中返回每个文件的第一个匹配项

MySQL:每个选项的第一个匹配项

NSPredicate仅查找第一个匹配项

WinRAR批处理文件,用于将每个文件夹中的第一个子目录压缩为一个zip文件

Bash命令从每个文件夹递归获取第一个和最后一个文件

在AHK中打开文件夹中的第一个文件的方法

如果文件夹长度超过限制,则删除文件夹中的第一个文件

如何转换for循环以查找Java流中的第一个匹配项?

在Pandas数据框中查找第一个匹配项

在1和0的字符串中查找0的第一个匹配项

在Kotlin的嵌套列表中查找并返回第一个匹配项?

在列表列表中向后查找第一个匹配项

Powershell从文件夹中捕获第一个文件名

如何打开文件夹中的第一个文件

打开递归文件夹列表中的第一个文件

将第一个文件创建到文件夹中并运行它 ssh

从目录中的所有文件夹名称中删除第一个单词

删除字符串中的第一个文件夹

删除路径中的第一个文件夹

ListBlobs() 只能看到 Azure blob 容器中的第一个文件夹

批处理:打开目录中的第一个文件夹