Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 325
db1 Avatar asked Jan 15 '14 16:01

db1


People also ask

How do we get the count of number of files in a directory?

Browse to the folder containing the files you want to count. Highlight one of the files in that folder and press the keyboard shortcut Ctrl + A to highlight all files and folders in that folder. In the Explorer status bar, you'll see how many files and folders are highlighted, as shown in the picture below.

How do I count files using grep?

Using grep -c alone will count the number of lines that contain the matching word instead of the number of total matches. The -o option is what tells grep to output each match in a unique line and then wc -l tells wc to count the number of lines. This is how the total number of matching words is deduced.

How do I find the pattern of a directory in Unix?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in.


1 Answers

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 -m 10000  real    0m0.034s user    0m0.017s sys     0m0.021s  $ time ls -1 | grep 20061101-20131101_kh5x7tte9n_201 | wc -m 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 -m 10000  real    0m0.023s user    0m0.020s sys     0m0.012s 
like image 97
fedorqui 'SO stop harming' Avatar answered Sep 22 '22 18:09

fedorqui 'SO stop harming'