繁体   English   中英

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

[英]find folders in a directory, without listing the parent directory

列出我不在的文件夹的内容时遇到问题,同时排除实际的文件夹名称本身。

例如:

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

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

思考?

简单就没有错

find ~/test -mindepth 1

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

find ~/test/*

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

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

您可以使用-execbasename

find ~/test -type d -exec basename {} \;

说明:

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

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

或者,如果要显示文件夹列表, -mindepth 1包括父级-mindepth 1find ~/test -type d -mindepth 1 )。

现在你编辑了它,我想你想要的是什么

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

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

我只是用sed修复它

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

其中$BASE是最初的foldername。

暂无
暂无

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

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