简体   繁体   English

Linux CLI 找到没有子目录的目录路径 - 结果只有父路径

[英]Linux CLI find dir paths without their children - only parent paths as a result

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和 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.我想问一下如何使用find命令来获得仅包含与模式匹配的父路径的所需结果。

I need to use -ipath switch so I can match paths with more conditions but still only the parent paths are desired result.我需要使用-ipath开关,以便我可以匹配具有更多条件的路径,但仍然只有父路径是所需的结果。

I'm using sh and bash.我正在使用 sh 和 bash。

Use -prune .使用-prune I used your command to replicate the dir tree and issued your commands with -prune , which resulted in:我使用您的命令复制目录树并使用-prune发出您的命令,结果是:

$ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM