bash 如何查找和计算与给定字符串匹配的文件数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9397816/
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
How can I find and count number of files matching a given string?
提问by Katherine Rix
I want to find and count all the files on my system that begin with some string, say "foo", using only one linein bash.
我想在我的系统上查找并计算所有以某个字符串开头的文件,比如“foo”,在 bash 中只使用一行。
I'm new to bash so I'd like to avoid scripting if possible - how can I do this using only simple bash commands and maybe piping in just one line?
我是 bash 的新手,所以如果可能的话,我想避免编写脚本 - 我怎样才能只使用简单的 bash 命令并可能只在一行中使用管道来做到这一点?
So far I've been using find / -name foo*. This returns the list of files, but I don't know what to add to actually count the files.
到目前为止,我一直在使用find / -name foo*. 这将返回文件列表,但我不知道要添加什么来实际计算文件数。
回答by Adam Liss
You can use
您可以使用
find / -type f -name 'foo*' | wc -l
- Use the single-quotes to prevent the shell from expanding the asterisk.
- Use
-type fto include only files (not links or directories). wc -lmeans "word count, lines only." Sincefindwill list one file per line, this returns the number of files it found.
- 使用单引号防止外壳扩展星号。
- 用于
-type f仅包含文件(而不是链接或目录)。 wc -l意思是“字数统计,仅行”。由于find将每行列出一个文件,这将返回它找到的文件数。
回答by parapura rajkumar
回答by Bernhard
You can pipe it into wc
你可以把它管成 wc
find / -name foo * | wc -l

