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.
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