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

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


It might be better to use find for this:

find . -name "pattern_*" -printf '.' | wc -m

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 -m10000real    0m0.034suser    0m0.017ssys     0m0.021s$ time ls -1 | grep 20061101-20131101_kh5x7tte9n_201 | wc -m10000real    0m0.254suser    0m0.245ssys     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 -m10000real    0m0.023suser    0m0.020ssys     0m0.012s


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


Try this:

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