简体   繁体   English

如何查找和计算与给定字符串匹配的文件数?

[英]How can I find and count number of files matching a given string?

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. 我想找到并计算我的系统中所有以字符串开头的文件,比如“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* . 到目前为止,我一直在使用find / -name foo* This returns the list of files, but I don't know what to add to actually count the files. 这将返回文件列表,但我不知道要添加什么来实际计算文件。

You can use 您可以使用

find / -type f -name 'foo*' | wc -l
  • Use the single-quotes to prevent the shell from expanding the asterisk. 使用单引号可防止shell扩展星号。
  • Use -type f to include only files (not links or directories). 使用-type f仅包含文件(不包括链接或目录)。
  • wc -l means "word count, lines only." wc -l表示“单词计数,仅限行数”。 Since find will list one file per line, this returns the number of files it found. 由于find将每行列出一个文件,因此返回它找到的文件数。

find / -name foo* | wc -l find / -name foo* | wc -l should do it. find / -name foo* | wc -l应该这样做。 Here is a link to man wc . 这是man wc的链接。 wc -l counts the number of lines wc -l计算行数

你可以把它管道到wc

find / -name foo * | wc -l

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算包含特定字符串但不包含另一个字符串的文件数? - How can I count the number of files containing a specific string but not containing another string? 如何找到两个匹配文件的总数 - how to find total count of two matching files 如何查找和移动与给定文件开头(BOF)字符串匹配的所有文件? - How to find and move all files matching given Beginning Of File (BOF) string? 给定一个带有文件名的数组,如何在 bash 中查找并删除所有匹配的文件? - Given an array with filenames, how to find and delete all matching files in bash? Python脚本根据文件中的匹配字符串对文件夹中的文件数进行计数 - Python script to count number of files in a folder based on matching string within the files Bash 脚本:如何修补文件? (在文件的给定位置写入给定字符串) - Bash scripting: How can I patch files? (write a given string in a given position of a file) 如何为给定的字符串找到一个字符串和不确定的上下行数 - how to find a string and an indeterminate number of lines up and down for the string given 包含给定字符串的文件数 - Number of files containing a given string 在 linux 的目录中查找与模式匹配的文件数 - Find count of files matching a pattern in a directory in linux 计算HDFS文件夹上具有给定扩展名的文件数 - Count number of files with given extension on HDFS folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM