ts0*_*s01 10 command-line directory find directory-structure
有没有办法找到给定目录树的最大深度?我正在考虑使用 find 增加 maxdepth 并比较找到的目录数量,但也许有更简单的方法?
Sat*_*ura 16
一种方法来做到这一点,假设 GNU find
:
find . -type d -printf '%d\n' | sort -rn | head -1
Run Code Online (Sandbox Code Playgroud)
这不是特别有效,但肯定比-maxdepth
依次尝试不同的s好得多。
试试这个,例如尝试在 下找到树的最大深度/
,使用
find / -type d
Run Code Online (Sandbox Code Playgroud)
/
无论深度如何,都会给出每个目录。所以awk
结果用/
作为定界符来找到计数,并且count-1
会给出最大的树深度/
,所以命令是:
find / -type d | awk -F"/" 'NF > max {max = NF} END {print max}'
Run Code Online (Sandbox Code Playgroud)