bash 在目录中查找文件夹,而不列出父目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13384922/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find folders in a directory, without listing the parent directory
提问by cbcp
Having trouble listing the contents of a folder I'm not in, while excluding the actual folder name itself.
无法列出我不在的文件夹的内容,同时排除实际的文件夹名称本身。
ex:
前任:
root@vps [~]# find ~/test -type d
/root/test
/root/test/test1
However I want it to only display /test1, as the example.
但是我希望它只显示 /test1,作为示例。
Thoughts?
想法?
采纳答案by sampson-chen
You can do that with -execand basename:
你可以用-execand做到这一点basename:
find ~/test -type d -exec basename {} \;
Explanation:
解释:
- The find ~/test -type dpart finds all directories recursively under~/test, as you already know.
- The -exec basename {} \;part runs thebasenamecommand on{}, which is where all the results from the last step are substituted into.
- 正如您已经知道的那样,该find ~/test -type d部分递归地查找 下的所有目录~/test。
- 该-exec basename {} \;部分在basename上运行命令{},这是将上一步的所有结果代入的地方。
回答by sanmiguel
There's nothing wrong with a simple
一个简单的没有错
find ~/test -mindepth 1
Similarly, this will have the same effect:
同样,这将产生相同的效果:
find ~/test/*
as it matches everything contained within ~/test/but not ~/testitself.
因为它匹配包含在其中~/test/但不匹配~/test自身的所有内容。
As an aside, you'll almost certainly find that findwill complain about the -mindepth noption being after any other switches, as ordering is normally important but the -(min|max)depth nswitches affect overall behaviour.
顺便说一句,您几乎肯定会发现find会抱怨该-mindepth n选项在任何其他开关之后,因为顺序通常很重要,但-(min|max)depth n开关会影响整体行为。
回答by Michael Krelin - hacker
Then you need -type finstead of -type d.
那么你需要-type f代替-type d.
Or, if you want to display list of folders, excluding the parent -mindepth 1(find ~/test -type d -mindepth 1).
或者,如果要显示文件夹列表,不包括父-mindepth 1( find ~/test -type d -mindepth 1)。
And now that you edited it, I think what you want may be
现在你编辑了它,我想你想要的可能是
find ~/test -type d -mindepth 1 |cut -d/ -f3-
But I think you need to be more specific ;-)
但我认为你需要更具体;-)
回答by cbcp
I just fixed it with sed
我只是用 sed
find $BASE -type d \( ! -iname "." \)|sed s/$BASE//g
Where $BASEis initial foldername. 
$BASE初始文件夹名在哪里。

