2

I have this dir tree:

/tmp/find-test/
├── one
│   ├── abc
│   │   ├── one
│   │   └── two
│   └── def
│       ├── one
│       └── two
└── two
    ├── abc
    │   ├── one
    │   └── two
    └── def
        ├── one
        └── two

and the same for find-test2

/tmp/find-test2/
├── one
│   ├── abc
│   │   ├── one
│   │   └── two
│   └── def
│       ├── one
│       └── two
└── two
    ├── abc
    │   ├── one
    │   └── two
    └── def
        ├── one
        └── two

If I issue:

find /tmp -ipath "*find-test*" -type d

I get:

/tmp/find-test
/tmp/find-test/two
/tmp/find-test/two/def
/tmp/find-test/two/def/two
/tmp/find-test/two/def/one
...
/tmp/find-test2
/tmp/find-test2/two
/tmp/find-test2/two/def
/tmp/find-test2/two/def/two
/tmp/find-test2/two/def/one
...

but I would like to get as a result only parent dirs without their children. The desired result should be just these two parent paths:

/tmp/find-test
/tmp/find-test2

and similarly if I issue command:

find /tmp -ipath "*one*" -type d

I would like to get only:

/tmp/find-test/one
/tmp/find-test/two/abc/one
/tmp/find-test/two/def/one
/tmp/find-test2/one
/tmp/find-test2/two/abc/one
/tmp/find-test2/two/def/one

Please note it is without for example:

/tmp/find-test/one/abc/one

since it is already held by

/tmp/find-test/one

script to replicate dir trees:

mkdir -p /tmp/find-test1/{one,two}/{abc,def}/{one,two}
mkdir -p /tmp/find-test2/{one,two}/{abc,def}/{one,two}

and find commands:

find /tmp -ipath "*find-test*" -type d
find /tmp -ipath "*one*" -type d

I would like to ask how to use the find command to get the desired result containing only parent paths matching the pattern.

I need to use -ipath switch so I can match paths with more conditions but still only the parent paths are desired result.

I'm using sh and bash.

Jimmix
  • 5,644
  • 6
  • 44
  • 71
  • Use `-iname` instead of `-ipath` – Barmar Apr 10 '20 at 23:14
  • Why do you think the shell you're using matters? The `find` command is not part of the shell. – Barmar Apr 10 '20 at 23:14
  • [so] is for programming questions, not questions about using or configuring Unix and its utilities. [unix.se] or [su] would be better places for questions like this. – Barmar Apr 10 '20 at 23:15
  • @Barmar I thought I would need to wrap find command with a script – Jimmix Apr 10 '20 at 23:15
  • @Barmar is it possible to do it by using `-ipath`? I provided the most elementary example and there are some cases where I need to find only parent dirs but there are more wildcards in the path for example `/tmp/bar*/foo*/*baz*/` – Jimmix Apr 10 '20 at 23:20
  • You can use both `-ipath` and `-iname`. `-ipath` applies to the entire path, `-iname` applies to the final name in the path. – Barmar Apr 10 '20 at 23:23
  • So `-ipath 'bar*/foo*' -iname '*baz*'` – Barmar Apr 10 '20 at 23:23
  • @Barmar but it returns all matching paths including children instead of just parent paths, for example in my case `find /tmp -ipath "*one*" -iname "*one*" -type d` returns 10 results instead of just four `/tmp/find-test/one`, `/tmp/find-test/two/abc/one`, `/tmp/find-test2/one`, `/tmp/find-test2/two/abc/one` – Jimmix Apr 10 '20 at 23:29
  • @Barmar Ad: `find /tmp -iname "*one*" -type d` does not work as well as desired: returnsl also 10 paths instead of 4. But as mentioned i need to use `-ipath` instead of just `-iname` anyway – Jimmix Apr 10 '20 at 23:31
  • I see my misunderstanding. I didn't notice that you have `one` nested inside another `one`, and don't want both of them. – Barmar Apr 10 '20 at 23:36
  • @Barmar You were right, there was no need to write any script :) – Jimmix Apr 10 '20 at 23:45

1 Answers1

3

Use -prune. I used your command to replicate the dir tree and issued your commands with -prune, which resulted in:

$ find 2>&- /tmp -ipath '*find-test*' -type d -prune
/tmp/find-test2
/tmp/find-test1
$ find 2>&- /tmp -ipath '*one*' -type d -prune
/tmp/find-test2/two/def/one
/tmp/find-test2/two/abc/one
/tmp/find-test2/one
/tmp/find-test1/two/def/one
/tmp/find-test1/two/abc/one
/tmp/find-test1/one
KamilCuk
  • 120,984
  • 8
  • 59
  • 111