Thursday, May 16, 2024
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 924  / 2 Years ago, thu, february 3, 2022, 4:38:45

Given the following folder layout:



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


How can I use find (or any other tool) to produce an output similar to:



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


Meaning that the search is stopped after a match with the literal "CMakeLists.txt" for each recursive folder branch respectively.


More From » command-line

 Answers
5

What worked for me was the following bash script:



#!/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's answer worked for the minimal example above as well. Using it on my actual folder structure (~4000 folders) it was still giving wrong results. I'm not sure why, to be honest.



When I tried @muru's solution it wouldn't stop executing. I guess the function calling itself isn't aborting properly? Not sure about the reason here either.



PS: In an actual CMake Project the file is actually called "CMakeLists.txt" (note the extra s in the end). I spotted this error only after answers were already provided.


[#8898] Saturday, February 5, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rowbris

Total Points: 419
Total Questions: 122
Total Answers: 101

Location: Norway
Member since Mon, May 23, 2022
2 Years ago
;