在linux中的目录中查找与模式匹配的文件数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21143043/
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
Find count of files matching a pattern in a directory in linux
提问by db1
I am new to linux. I have a directory in linux with approx 250,000 files I need to find count of number of files matching a pattern.
我是 linux 新手。我在 linux 中有一个目录,里面有大约 250,000 个文件,我需要找到匹配模式的文件数。
I tried using following command :
我尝试使用以下命令:
ls -1 20061101-20131101_kh5x7tte9n_2010_* | wc -l
I got the following error message:
我收到以下错误消息:
-bash: /bin/ls: Argument list too long
0
Please help. Thanks in advance
请帮忙。提前致谢
回答by fedorqui 'SO stop harming'
It might be better to use find
for this:
find
用于此目的可能会更好:
find . -name "pattern_*" -printf '.' | wc -l
In your specific case:
在您的具体情况下:
find . -maxdepth 1 -name "20061101-20131101_kh5x7tte9n_2010_*" -printf '.' | wc -m
find
will return a list of files matching the criteria. -maxdepth 1
will make the search to be done just in the path, no subdirectories (thanks Petesh!). -printf '.'
will print a dot for every match, so that names with new lines won't make wc -m
break.
find
将返回符合条件的文件列表。-maxdepth 1
将使搜索仅在路径中完成,没有子目录(感谢 Petesh!)。-printf '.'
将为每个匹配打印一个点,以便带有新行的名称不会wc -m
中断。
Then wc -m
will indicate the number of characters which will match the number of files.
然后wc -m
将指示与文件数匹配的字符数。
Performance comparation of two possible options:
两种可能选项的性能比较:
Let's create 10 000 files with this pattern:
让我们用这种模式创建 10 000 个文件:
$ for i in {1..10000}; do touch 20061101-20131101_kh5x7tte9n_201_$i; done
And then compare the time it takes to get the result with ls -1 ...
or find ...
:
然后将获得结果所需的时间与ls -1 ...
or 进行比较find ...
:
$ time find . -maxdepth 1 -name "20061101-20131101_kh5x7tte9n_201_*" | wc -l
10000
real 0m0.034s
user 0m0.017s
sys 0m0.021s
$ time ls -1 | grep 20061101-20131101_kh5x7tte9n_201 | wc -l
10000
real 0m0.254s
user 0m0.245s
sys 0m0.020s
find
is x5 times faster! But if we use ls -1f
(thanks Petesh again!), then ls
is even faster than find
:
find
快 x5 倍!但是如果我们使用ls -1f
(再次感谢 Petesh!),那么ls
甚至比find
:
$ time ls -1f | grep 20061101-20131101_kh5x7tte9n_201 | wc -l
10000
real 0m0.023s
user 0m0.020s
sys 0m0.012s
回答by Dale
Try this:
尝试这个:
ls -1 | grep 20061101-20131101_kh5x7tte9n_2010_ | wc -l
回答by Odobenus Rosmarus
you got "argument too long" because shell expands your pattern to the list of files. try:
你得到了“参数太长”,因为 shell 将你的模式扩展到文件列表。尝试:
find -maxdepth 1 -name '20061101-20131101_kh5x7tte9n_2010_*' |wc -l
please pay attention - pattern is enclosed in quotes to prevent shell expansion
请注意 - 模式用引号括起来以防止外壳扩展
回答by Jas
ls -1 | grep '20061101-20131101_kh5x7tte9n_2010_*' | wc -l
Previous answer did not included quotes around search criteria neither * wildcard.
先前的答案不包括搜索条件周围的引号,也不包括 * 通配符。
回答by tripleee
You should generally avoid ls
in scriptsand in fact, performing the calculation in a shell function will avoid the "argument list too long" error because there is no exec
boundary and so the ARGV_MAX
limit doesn't come into play.
您通常应该避免ls
在脚本中,事实上,在 shell 函数中执行计算将避免“参数列表太长”错误,因为没有exec
边界,因此ARGV_MAX
限制不会起作用。
number_of_files () {
if [ -e "" ]; then
echo "$#"
else
echo 0
fi
}
The conditional guards against the glob not being expanded at all (which is the default out of the box; in Bash, you can shopt -s nullglob
to make wildcards which don't match any files get expanded into the empty string).
条件防止 glob 根本没有被扩展(这是开箱即用的默认设置;在 Bash 中,您可以shopt -s nullglob
将不匹配任何文件的通配符扩展为空字符串)。
Try it:
尝试一下:
number_of_files 20061101-20131101_kh5x7tte9n_2010_*
回答by Tal Ater
The MacOS / OS X command line solution
MacOS/OS X 命令行解决方案
If you are attempting to do this in the command line on a Mac you will soon find out that find
does not supportthe -printf
option.
如果您尝试在 Mac 上的命令行中执行此操作,您很快就会发现该选项find
不支持-printf
。
To accomplish the same result as the solution proposed by fedorqui-supports-monicatry this:
要实现与fedorqui-supports-monica提出的解决方案相同的结果,请尝试以下操作:
find . -name "pattern_*" -exec stat -f "." {} \; | wc -l
This will find all files matching the pattern you entered, print a .
for each of them in a newline, then finally count the number of lines and output that number.
这将找到与您输入的模式匹配的所有文件,.
在换行符中为每个文件打印一个,然后最后计算行数并输出该数字。
To limit your search depth to the current directory, add -maxdepth 1
to the command like so:
要将搜索深度限制到当前目录,请添加-maxdepth 1
到命令中,如下所示:
find . -maxdepth 1 -name "196288.*" -exec stat -f "." {} \; | wc -l