Friday, January 26, 2024

[SOLVED] regex to find path under parent directory

Issue

Team,

I have this expression that finds all the bazel but I want to list only the parent dir that has it. how could i?

find src/services/ -type f -name 'BUILD.bazel' | sed -r 's|/[^/]||' |sort |uniq |grep -e etl-domain/

output

srcervices/etl-domain/api/BUILD.bazel
srcervices/etl-domain/BUILD.bazel

expected output

srcervices/etl-domain/BUILD.bazel

Solution

find has a maxdepth parameter, which makes it possible to define the depth of your search. In order just to see the subdirectories, but not deeper, you can use:

find <directory> -maxdepth 1 ...

In your case, this becomes:

find src/services/ -maxdepth 1 -type f -name 'BUILD.bazel' ...


Answered By - Dominique
Answer Checked By - Terry (WPSolving Volunteer)