0

How can I search the filesystem only for the first positive match in a path? For example, searching for foo:

./some/foo/long/foo/path
./another/foo/long/foo/path

should return:

./some/foo/
./another/foo/

I think the find command should be able to do this, but I can't figure out which flags to use, obviously any other command that achieves the result would be fine.

maja
  • 329
  • 6
  • 15

1 Answers1

2

Find directories named foo and -prune them.

find . -type d -name foo -prune

find does not add trailing slashses. If you really need them then:

find . -type d -name foo -prune -exec printf '%s/\n' {} +

Your find may or may not support -printf primary. If it does then you can avoid calling an external printf executable. E.g. with GNU find:

find . -type d -name foo -prune -printf '%p/\n'
Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
  • this is a good answer and I made some edits to the question to clarify the few points it raised – maja May 27 '21 at 08:43
  • 1
    @maja Answer adjusted. The difference is e.g. in `./some/bar/long/foo/path`, if `./some/foo/` exists. The previous answer would tread `some` as a "directory that contains name", so `some` would be "a match" and "without searching matches' subfolders" would mean we don't want to find `./some/bar/long/foo`. The current answer treats `foo` as "a match" and it finds `./some/bar/long/foo`. – Kamil Maciorowski May 27 '21 at 08:55