递归计算Linux目录中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9157138/
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
Recursively counting files in a Linux directory
提问by Robert Buckley
How can I recursively count files in a Linux directory?
如何递归计算 Linux 目录中的文件?
I found this:
我找到了这个:
find DIR_NAME -type f | wc -l
But when I run this it returns the following error.
但是当我运行它时,它返回以下错误。
find: paths must precede expression: |
查找:路径必须在表达式之前:|
采纳答案by paulsm4
This should work:
这应该有效:
find DIR_NAME -type f | wc -l
Explanation:
解释:
-type f
to include only files.|
(and not|
) redirectsfind
command's standard output towc
command's standard input.wc
(short for word count) counts newlines, words and bytes on its input (docs).-l
to count just newlines.
-type f
只包含文件。|
(而不是|
) 将find
命令的标准输出重定向到wc
命令的标准输入。wc
(单词计数的缩写)计算其输入(docs)上的换行符、单词和字节数。-l
只计算换行符。
Notes:
笔记:
- Replace
DIR_NAME
with.
to execute the command in the current folder. - You can also remove the
-type f
to include directories (and symlinks) in the count. - It's possible this command will overcount if filenames can contain newline characters.
- 更换
DIR_NAME
用.
在当前文件夹要执行的命令。 - 您还可以删除
-type f
计数中的包含目录(和符号链接)。 - 如果文件名可以包含换行符,则此命令可能会多计。
Explanation of why your example does not work:
解释为什么您的示例不起作用:
In the command you showed, you do not use the "Pipe" (|
) to kind-of connect two commands, but the broken bar (|
) which the shell does not recognize as a command or something similar. That's why you get that error message.
在您显示的命令中,您没有使用“管道”( |
) 来连接两个命令,而是使用断线 ( |
),shell 无法将其识别为命令或类似的东西。这就是您收到该错误消息的原因。
回答by Abhishek Maurya
For the current directory:
对于当前目录:
find -type f | wc -l
回答by Sophy
To determine how many files there are in the current directory, put in ls -1 | wc -l
. This uses wc
to do a count of the number of lines (-l)
in the output of ls -1
. It doesn't count dotfiles. Please note that ls -l
(that's an "L" rather than a "1" as in the previous examples) which I used in previous versions of this HOWTO will actually give you a file count one greater than the actual count. Thanks to Kam Nejad for this point.
要确定当前目录中有多少文件,请将ls -1 | wc -l
. 这用于wc
计算(-l)
的输出中的行数ls -1
。它不计算点文件。请注意ls -l
(这是一个“L”而不是前面例子中的“1”)我在本 HOWTO 的先前版本中使用的实际上会给你一个比实际计数大 1 的文件计数。感谢 Kam Nejad 在这一点上。
If you want to count only files and NOT include symbolic links (just an example of what else you could do), you could use ls -l | grep -v ^l | wc -l
(that's an "L" not a "1" this time, we want a "long" listing here). grep
checks for any line beginning with "l" (indicating a link), and discards that line (-v).
如果你只想计算文件ls -l | grep -v ^l | wc -l
而不包括符号链接(只是你可以做的其他事情的一个例子),你可以使用(这次是“L”而不是“1”,我们想要一个“长”列表) . grep
检查以“l”(表示链接)开头的任何行,并丢弃该行 (-v)。
Relative speed: "ls -1 /usr/bin/ | wc -l" takes about 1.03 seconds on an unloaded 486SX25 (/usr/bin/ on this machine has 355 files). "ls -l /usr/bin/ | grep -v ^l | wc -l
" takes about 1.19 seconds.
相对速度:“ls -1 /usr/bin/ | wc -l”在卸载的 486SX25 上大约需要 1.03 秒(此机器上的 /usr/bin/ 有 355 个文件)。" ls -l /usr/bin/ | grep -v ^l | wc -l
"大约需要 1.19 秒。
Source: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x700.html
回答by Greg Bell
If you want a breakdown of how many files are in each dir under your current dir:
如果您想了解当前目录下每个目录中有多少文件:
for i in */ .*/ ; do
echo -n $i": " ;
(find "$i" -type f | wc -l) ;
done
That can go all on one line, of course. The parenthesis clarify whose output wc -l
is supposed to be watching (find $i -type f
in this case).
当然,这可以在一条线上完成。括号阐明了wc -l
应该监视谁的输出(find $i -type f
在这种情况下)。
回答by lev
You can use
您可以使用
$ tree
after installing the treepackage with
安装树包后
$ sudo apt-get install tree
(on a Debian / Mint / Ubuntu Linux machine).
(在 Debian / Mint / Ubuntu Linux 机器上)。
The command shows not only the count of the files, but also the count of the directories, separately. The option -L can be used to specify the maximum display level (which, by default, is the maximum depth of the directory tree).
该命令不仅显示文件数,还分别显示目录数。选项 -L 可用于指定最大显示级别(默认情况下,这是目录树的最大深度)。
Hidden files can be included too by supplying the -a
option .
通过提供-a
选项,也可以包含隐藏文件。
回答by Santrix
If you want to know how many files and sub-directories exist from the present working directory you can use this one-liner
如果你想知道当前工作目录中有多少文件和子目录,你可以使用这个单行
find . -maxdepth 1 -type d -print0 | xargs -0 -I {} sh -c 'echo -e $(find {} | wc -l) {}' | sort -n
This will work in GNU flavour, and just omit the -e from the echo command for BSD linux (e.g. OSX).
这将适用于 GNU 风格,并且只需从 BSD linux(例如 OSX)的 echo 命令中省略 -e。
回答by Reinstate Monica Please
If you want to avoid error cases, don't allow wc -l
to see files with newlines (which it will count as 2+ files)
如果您想避免错误情况,请不要wc -l
查看带有换行符的文件(这将计为 2 个以上的文件)
e.g. Consider a case where we have a single file with a single EOL character in it
例如,考虑这样一种情况,其中我们有一个包含单个 EOL 字符的文件
> mkdir emptydir && cd emptydir
> touch $'file with EOL(\n) character in it'
> find -type f
./file with EOL(?) character in it
> find -type f | wc -l
2
Since at least gnu wc
does not appear to have an option to read/count a null terminated list (except from a file), the easiest solution would just be to not pass it filenames, but a static output each time a file is found, e.g. in the same directory as above
由于至少 gnuwc
似乎没有读取/计数空终止列表的选项(除了从文件中),最简单的解决方案就是不传递文件名,而是每次找到文件时静态输出,例如在与上面相同的目录中
> find -type f -exec printf '\n' \; | wc -l
1
Or if your find
supports it
或者如果你find
支持它
> find -type f -printf '\n' | wc -l
1
回答by Sebastian Meine
Combining several of the answers here together, the most useful solution seems to be:
将这里的几个答案结合在一起,最有用的解决方案似乎是:
find . -maxdepth 1 -type d -print0 |
xargs -0 -I {} sh -c 'echo -e $(find "{}" -printf "\n" | wc -l) "{}"' |
sort -n
It can handle odd things like file names that include spaces parenthesis and even new lines. It also sorts the output by the number of files.
它可以处理奇怪的事情,比如包含空格括号甚至换行的文件名。它还按文件数对输出进行排序。
You can increase the number after -maxdepth
to get sub directories counted too. Keep in mind that this can potentially take a long time, particularly if you have a highly nested directory structure in combination with a high -maxdepth
number.
您也可以在-maxdepth
计算子目录之后增加数量。请记住,这可能需要很长时间,特别是如果您有一个高度嵌套的目录结构和-maxdepth
大量的目录结构。
回答by DanielK
This alternate approach with filtering for format counts all available grub kernel modules:
这种过滤格式的替代方法计算所有可用的 grub 内核模块:
ls -l /boot/grub/*.mod | wc -l
回答by psmith
On my computer, rsync
is a little bit faster than find | wc -l
in the accepted answer:
在我的电脑上,rsync
比find | wc -l
接受的答案快一点:
$ rsync --stats --dry-run -ax /path/to/dir /tmp
Number of files: 173076
Number of files transferred: 150481
Total file size: 8414946241 bytes
Total transferred file size: 8414932602 bytes
The second line has the number of files, 150,481 in the above example. As a bonus you get the total size as well (in bytes).
第二行是文件数,在上例中为 150,481。作为奖励,您还可以获得总大小(以字节为单位)。
Remarks:
评论:
- the first line is a count of files, directories, symlinks, etc all together, that's why it is bigger than the second line.
- the
--dry-run
(or-n
for short) option is important to not actually transfer the files! - I used the
-x
option to "don't cross filesystem boundaries", which means if you execute it for/
and you have external hard disks attached, it will only count the files on the root partition.
- 第一行是文件、目录、符号链接等的计数,这就是为什么它比第二行大。
- 在
--dry-run
(或-n
简称)选项没有实际传输文件是很重要的! - 我使用了
-x
“不跨越文件系统边界”的选项,这意味着如果你执行它/
并且你连接了外部硬盘,它只会计算根分区上的文件。