10

I am trying to use find (or a similar tool, I also really like fd)

  • to locate directories matching the query

  • only those directories which are the deepest match

  • the target directories are not all the same depth, so -maxdepth or -mindepth can't do it

  • it would be the opposite of -prune

Given the below directory tree, looking for directories with "red" in the name, the ones with Yes should be the matches:

$ tree
.
├── blue                = n
├── green               = n
│   ├── red-apple       = Yes
│   └── red-book        = n
│       └── red-pen     = Yes
├── red                 = n
│   └── red-strawberry  = Yes
│   └── 
└── yellow              = n
    └── basket          = n
        ├── flag        = n
        │   └── red     = Yes
        └── painted-red = Yes
            └── wagon   = n

So "red-book" does not match because it contains a subsequent match ("red-pen"). But it's OK to have a deeper non-matching directory (like "painted-red").

This is my basic command which finds 2 unwanted items:

$ find . -type d -name "*red*"
./green/red-apple
./green/red-book
./green/red-book/red-pen   # wrong
./yellow/basket/flag/red
./yellow/basket/red
./red                      # wrong
./red/red-strawberry
no comments (yet)
sorted by: hot top new old
there doesn't seem to be anything here
this post was submitted on 07 Sep 2026
10 points (100.0% liked)

Linux Questions

4165 readers
1 users here now

Linux questions Rules (in addition of the Lemmy.zip rules)

Tips for giving and receiving help

Any rule violations will result in disciplinary actions

founded 3 years ago
MODERATORS