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

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'