Linux 计算特定目录中的目录数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17648033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
counting number of directories in a specific directory
提问by user2566898
How to count the number of folders in a specific directory. I am using the following command, but it always provides an extra one.
如何计算特定目录中的文件夹数量。我正在使用以下命令,但它总是提供一个额外的命令。
find /directory/ -maxdepth 1 -type d -print| wc -l
For example, if I have 3 folders, this command provides 4. If it contains 5 folders, the command provides 6. Why is that?
例如,如果我有 3 个文件夹,此命令提供 4 个。如果它包含 5 个文件夹,则该命令提供 6 个。为什么会这样?
回答by Pavel Anossov
find
is also printing the directory itself:
find
也在打印目录本身:
$ find .vim/ -maxdepth 1 -type d
.vim/
.vim/indent
.vim/colors
.vim/doc
.vim/after
.vim/autoload
.vim/compiler
.vim/plugin
.vim/syntax
.vim/ftplugin
.vim/bundle
.vim/ftdetect
You can instead test the directory's children and do not descend into them at all:
您可以改为测试目录的子项,并且根本不进入它们:
$ find .vim/* -maxdepth 0 -type d
.vim/after
.vim/autoload
.vim/bundle
.vim/colors
.vim/compiler
.vim/doc
.vim/ftdetect
.vim/ftplugin
.vim/indent
.vim/plugin
.vim/syntax
$ find .vim/* -maxdepth 0 -type d | wc -l
11
$ find .vim/ -maxdepth 1 -type d | wc -l
12
You can also use ls
:
您还可以使用ls
:
$ ls -l .vim | grep -c ^d
11
$ ls -l .vim
total 52
drwxrwxr-x 3 anossovp anossovp 4096 Aug 29 2012 after
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 autoload
drwxrwxr-x 13 anossovp anossovp 4096 Aug 29 2012 bundle
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 colors
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 compiler
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 doc
-rw-rw-r-- 1 anossovp anossovp 48 Aug 29 2012 filetype.vim
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftdetect
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftplugin
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 indent
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 plugin
-rw-rw-r-- 1 anossovp anossovp 2505 Aug 29 2012 README.rst
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 syntax
$ ls -l .vim | grep ^d
drwxrwxr-x 3 anossovp anossovp 4096 Aug 29 2012 after
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 autoload
drwxrwxr-x 13 anossovp anossovp 4096 Aug 29 2012 bundle
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 colors
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 compiler
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 doc
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftdetect
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftplugin
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 indent
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 plugin
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 syntax
回答by EmptyData
Get a count of only the directories in the current directory
仅获取当前目录中的目录数
echo */ | wc
echo */ | wc
you will get out put like 1 309 4594
你会像 1 309 4594
2nd digit
represents no. of directories.
2nd digit
代表没有。的目录。
or
或者
tree -L 1 | tail -1
tree -L 1 | tail -1
回答by Manish Shrivastava
Best way to navigate to your drive and simply execute
导航到您的驱动器并简单地执行的最佳方式
ls -lR | grep ^d | wc -l
and to Find all folders in total, including subdirectories?
并找到所有文件夹,包括子目录?
find /mount/point -type d | wc -l
...or find all folders in the root directory (not including subdirectories)?
...或找到根目录中的所有文件夹(不包括子目录)?
find /mount/point -maxdepth 1 -type d | wc -l
Cheers!
干杯!
回答by shiyani suresh
find . -mindepth 1 -maxdepth 1 -type d | wc -l
For find -mindepth
means total number recusive in directories
对于 find-mindepth
表示目录中递归的总数
-maxdepth
means total number recusive in directories
-maxdepth
表示目录中递归的总数
-type d
means directory
-type d
表示目录
And for wc -l
means count the lines of the input
而对于wc -l
意味着计算输入的行数
回答by gniourf_gniourf
A pure bash solution:
一个纯粹的 bash 解决方案:
shopt -s nullglob
dirs=( /path/to/directory/*/ )
echo "There are ${#dirs[@]} (non-hidden) directories"
If you also want to count the hidden directories:
如果您还想计算隐藏目录:
shopt -s nullglob dotglob
dirs=( /path/to/directory/*/ )
echo "There are ${#dirs[@]} directories (including hidden ones)"
Note that this will also count links to directories. If you don't want that, it's a bit more difficult with this method.
请注意,这也将计算到目录的链接。如果你不想那样,用这种方法会有点困难。
Using find
:
使用find
:
find /path/to/directory -type d \! -name . -prune -exec printf x \; | wc -c
The trick is to output an x
to stdout each time a directory is found, and then use wc
to count the number of characters. This will count the number of all directories (including hidden ones), excluding links.
诀窍是x
每次找到目录时输出一个到标准输出,然后用于wc
计算字符数。这将计算所有目录(包括隐藏目录)的数量,不包括链接。
The methods presented here are all safe wrt to funny characters that can appear in file names (spaces, newlines, glob characters, etc.).
此处介绍的方法对于可能出现在文件名中的有趣字符(空格、换行符、glob 字符等)都是安全的。
回答by textral
Run stat -c %h folder
and subtract 2 from the result. This employs only a single subprocess as opposed to the 2 (or even 3) required by most of the other solutions here (typically find
plus wc
).
运行stat -c %h folder
并从结果中减去 2。这仅使用一个子流程,而不是此处大多数其他解决方案所需的 2 个(甚至 3 个)(通常为find
plus wc
)。
Using sh/bash:
使用 sh/bash:
cnt=$((`stat -c %h folder` - 2))
echo $cnt # 'echo' is a sh/bash builtin, not an additional process
cnt=$((`stat -c %h folder` - 2))
echo $cnt # 'echo' 是一个 sh/bash 内置程序,而不是一个额外的进程
Using csh/tcsh:
使用 csh/tcsh:
@ cnt = `stat -c %h folder` - 2
echo $cnt # 'echo' is a csh/tcsh builtin, not an additional process
@ cnt = `stat -c %h folder` - 2
echo $cnt # 'echo' 是一个 csh/tcsh 内建的,不是一个额外的进程
Explanation: stat -c %h folder
prints the number of hardlinks to folder, and each subfolder under foldercontains a ../ entry which is a hardlink back to folder. You must subtract 2 because there are two additional hardlinks in the count:
说明:stat -c %h folder
打印到文件夹的硬链接数,文件夹下的每个子文件夹都包含一个 ../ 条目,它是返回文件夹的硬链接。您必须减去 2,因为计数中有两个额外的硬链接:
- folder's own self-referential ./ entry, and
- folder's parent's link to folder
- 文件夹自己的自引用 ./ 条目,以及
- folder的父文件夹的链接
回答by briceburg
I think the easiest is
我认为最简单的是
ls -ld images/* | wc -l
where images
is your target directory. The -d flag limits to directories, and the -l flag will perform a per-line listing, compatible with the very familiar wc -l
for line count.
images
你的目标目录在哪里。-d 标志限制目录,-l 标志将执行每行列表,与非常熟悉wc -l
的行数兼容。
回答by Manoj
Best way to do it:
最好的方法:
ls -la | grep -v total | wc -l
This gives you the perfect count.
这为您提供了完美的计数。
回答by Anne van Rossum
Using zsh
:
使用zsh
:
a=(*(/N)); echo ${#a}
The N
is a nullglob, /
makes it match directories, #
counts. It will neatly cope with spaces in directory names as well as returning 0
if there are no directories.
这N
是一个 nullglob,/
使它匹配目录,#
计数。它将巧妙地处理目录名称中的空格,0
并在没有目录时返回。
回答by margenn
Count all files and subfolders, windows style:
统计所有文件和子文件夹,windows 样式:
dir=/YOUR/PATH;f=$(find $dir -type f | wc -l); d=$(find $dir -mindepth 1 -type d | wc -l); echo "$f Files, $d Folders"