如何查找和计算与给定字符串匹配的文件数? [英] How can I find and count number of files matching a given string?
本文介绍了如何查找和计算与给定字符串匹配的文件数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想查找并计算系统中以某个字符串开头的所有文件,例如 foo,仅使用bash中的一行。
I want to find and count all the files on my system that begin with some string, say "foo", using only one line in bash.
我是bash的新手,所以我想尽可能避免编写脚本-我该如何仅使用简单的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?
到目前为止,我一直在使用 find / -name foo *
。这将返回文件列表,但我不知道要添加多少以实际计数文件。
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 / -type f -name 'foo*' | wc -l
- 使用单引号防止外壳扩展
- 使用
型f
仅包含文件(不包括链接或目录)。 -
wc -l </ code>表示字数,仅行数。由于
查找
每行将列出一个文件,因此将返回找到的文件数。 - Use the single-quotes to prevent the shell from expanding the asterisk.
- Use
-type f
to include only files (not links or directories). wc -l
means "word count, lines only." Sincefind
will list one file per line, this returns the number of files it found.
这篇关于如何查找和计算与给定字符串匹配的文件数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文