查找目录中的文件夹,而不列出父目录

时间:2012-11-14 18:24:52

标签: linux bash find

无法列出我不在的文件夹的内容,同时排除实际的文件夹名称本身。

例如:

root@vps [~]# find ~/test -type d
/root/test
/root/test/test1

但我希望它只显示/ test1,作为示例。

思想?

4 个答案:

答案 0 :(得分:35)

简单的

没有错
find ~/test -mindepth 1

同样,这也会产生同样的效果:

find ~/test/*

因为它匹配~/test/但不包含~/test本身所包含的所有内容。

顺便说一句,您几乎肯定会发现find会抱怨-mindepth n选项在任何其他切换之后,因为排序通常很重要,但-(min|max)depth n开关会影响整体行为。

答案 1 :(得分:7)

您可以使用-execbasename

执行此操作
find ~/test -type d -exec basename {} \;

<强>解释

  • 正如您所知,find ~/test -type d部分在~/test下递归查找所有目录。
  • -exec basename {} \;部分在basename上运行{}命令,该命令将替换最后一步的所有结果。

答案 2 :(得分:3)

然后您需要-type f而不是-type d

或者,如果您要显示文件夹列表,请排除父-mindepth 1find ~/test -type d -mindepth 1)。

现在你编辑了它,我认为你想要的可能是

find ~/test -type d -mindepth 1 |cut -d/ -f3-

但我认为你需要更加具体; - )

答案 3 :(得分:-1)

我刚用sed

修复了它
find $BASE -type d \( ! -iname "." \)|sed s/$BASE//g

$BASE是最初的foldername。

相关问题