簡體   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