递归计数Linux目录中的文件 [英] Recursively counting files in a Linux directory

查看:90
本文介绍了递归计数Linux目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何递归计算Linux目录中的文件?

How can I recursively count files in a Linux directory?

我发现了:

find DIR_NAME -type f ¦ wc -l

但是当我运行它时,它会返回以下错误.

But when I run this it returns the following error.

查找:路径必须在表达式之前:

find: paths must precede expression: ¦

推荐答案

这应该有效:

find DIR_NAME -type f | wc -l

说明:

  • -type f仅包含文件.
  • |(而非¦ )将find命令的标准输出重定向到wc命令的标准输入.
  • wc(单词计数的缩写)对输入的换行符,单词和字节进行计数(文档).
  • -l仅计算换行符.
  • -type f to include only files.
  • | (and not ¦) redirects find command's standard output to wc command's standard input.
  • wc (short for word count) counts newlines, words and bytes on its input (docs).
  • -l to count just newlines.

注释:

  • .替换DIR_NAME以在当前文件夹中执行命令.
  • 您还可以删除-type f以在计数中包括目录(和符号链接).
  • 如果文件名可以包含换行符,则此命令可能会计数过多.
  • 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.

说明您的示例无效的原因:

在显示的命令中,您没有使用管道"(|)来连接两个命令,而是使用了断线(¦),shell无法将其识别为命令或其他内容.相似的.这就是为什么您收到该错误消息的原因.

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.

这篇关于递归计数Linux目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆