目录中的文件夹数(递归)

jml*_*jml 10 bash directory find

我想打印给定 CWD/当前目录中的文件夹数量(递归,不包括隐藏文件夹)。我可以使用什么命令或一系列命令来确定此信息?

Tim*_*tin 18

这将找到当前工作目录中非隐藏目录的数量:

ls -l | grep "^d" | wc -l
Run Code Online (Sandbox Code Playgroud)

编辑:

要使其递归,请使用以下-R选项ls -l

ls -lR | grep "^d" | wc -l
Run Code Online (Sandbox Code Playgroud)

  • @transistor1 好点。然而,在这种情况下,`grep "^d"` 只显示行首有 `d` 的条目。空白/非目录行不应在没有 `wc -l` 的情况下显示或使用 `wc -l` 计算。 (3认同)

Hau*_*ing 5

在 GNU 土地上:

find . -mindepth 1 -maxdepth 1 -type d -printf . | wc -c
Run Code Online (Sandbox Code Playgroud)

别处

find . -type d ! -name . -printf . -prune | wc -c
Run Code Online (Sandbox Code Playgroud)

在 bash 中:

shopt -s dotglob
count=0
for dir in *; do
  test -d "$dir" || continue
  test . = "$dir" && continue
  test .. = "$dir" && continue
  ((count++))
done
echo $count
Run Code Online (Sandbox Code Playgroud)