给定以下文件夹布局:
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] 删除。
我来说两句