[英]Count number of subdirectories ignoring parent directories
我想計算深度為2的子目錄數,但忽略bash腳本中深度為1的父目錄。
例:
root_dir ---|--ignore_dir ---|--count_dir
| |--count_dir ---|--ignore_dir
| |--ignore_dir
|--ignore_dir ---|--count_dir
|--count_dir
|--count_dir
有任何想法嗎?
你可以嘗試這樣的事情:
find /path/to/root_dir -maxdepth 2 -mindepth 2 -type d | wc -l
在這里,我們明確地將find
的深度限制為2(不多也不少)並列出所有目錄。 wc -l
計算find
命令輸出的行數。
注意:
如果您的文件夾名稱包含換行符,特定字符或異常編碼,則使用wc -l
將產生不正確的結果。
從這個問題中獲取靈感,您可以為找到的每個文件夾打印一個字符,並計算生成的字符數。
您可以使用以下代碼段:
find /path/to/root_dir -maxdepth 2 -mindepth 2 -type d -printf '.' | wc -c
另一種方法是通過globbing
ls /path/to/root_dir/*/*/ -d | wc -l
我發現這種類型的解決方案更容易記住,但它是不容易的可擴展“(例如很多deapth水平)作為find
酮
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.