-2

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
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
AhmFM
  • 1,552
  • 3
  • 23
  • 53
  • 2
    If you `want to list only the parent dir that has it`, the command will be: `find src/services/ -type f -name 'BUILD.bazel' | sed -r 's|/[^/]*$||' | sort -u | grep etl-domain` and the expected output should be: `src/services/etl-domain src/services/etl-domain/api`. – tshiono Apr 24 '23 at 07:56
  • 1
    What's this `srcervices` in your expected output? And if you want a file named `BUILD.bazel` in a directory named `src/services/etl-domain` why do you use `find`, `sed`, `sort`, `uniq` and `grep`? Why not just testing if it exists with `f="src/services/etl-domain/BUILD.bazel" && [ -f "$f" ] && echo "$f"`? – Renaud Pacalet Apr 24 '23 at 08:59
  • 2
    How is this different from [your previous question](https://stackoverflow.com/q/76049937/1745001) with the file name stuck on the end? – Ed Morton Apr 24 '23 at 10:54
  • Does this answer your question? [how to exclude printing in bash a line that has only parent path and exclude all subdir paths](https://stackoverflow.com/questions/76049937/how-to-exclude-printing-in-bash-a-line-that-has-only-parent-path-and-exclude-all) – dawg Apr 24 '23 at 12:49
  • it is different because in those questions there is BUILD.bazel not to be printed and in this question, I just wanted path to be printed keeping bazel as is. – AhmFM Apr 25 '23 at 06:53

1 Answers1

3

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' ...
Dominique
  • 16,450
  • 15
  • 56
  • 112