Find count of files matching a pattern in a directory in linux

大憨熊 提交于 2019-12-31 13:15:15

问题


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.

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


回答1:


It might be better to use find for this:

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.

Then wc -m will indicate the number of characters which will match the number of files.


Performance comparation of two possible options:

Let's create 10 000 files with this pattern:

$ 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 ...:

$ 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:

$ time ls -1f | grep 20061101-20131101_kh5x7tte9n_201 | wc -l
10000

real    0m0.023s
user    0m0.020s
sys     0m0.012s



回答2:


Try this:

ls -1 | grep 20061101-20131101_kh5x7tte9n_2010_ | wc -l



回答3:


you got "argument too long" because shell expands your pattern to the list of files. try:

find  -maxdepth 1 -name '20061101-20131101_kh5x7tte9n_2010_*' |wc -l

please pay attention - pattern is enclosed in quotes to prevent shell expansion




回答4:


You should generally avoid ls in scripts and 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.

number_of_files () {
    if [ -e "$1" ]; 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).

Try it:

number_of_files 20061101-20131101_kh5x7tte9n_2010_*



回答5:


ls -1 | grep '20061101-20131101_kh5x7tte9n_2010_*' | wc -l

Previous answer did not included quotes around search criteria neither * wildcard.



来源:https://stackoverflow.com/questions/21143043/find-count-of-files-matching-a-pattern-in-a-directory-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!