How can I count the total number of files


Transfered from Linux Config Disqus comments:


Rupesh Dev

How can I count the total number of files for following scenario ?
Files are named as 20_hello.jpg, 25_cat.jpg, 60_picture.jpg, 61_phone.jpg … 94_tower.jpg, 100_percent.jpg etc.

I want to count total number of .jpg files which starts with Initial number( 60_ to 100_). It means all labels above 60 to 100 has to be counted ?

Thanks !


Hi Rupesh,

I have created couple more files, so for example:

$ ls
100_percent.jpg  110_text.jpg  25_cat.jpg      61_phone.jpg     94_tower.jpg
10_text.jpg      20_hello.jpg  60_picture.jpg  623_another.jpg

One solution to this could be:

$ ls | grep -E "[6-9][[:digit:]]_|100_" 

then you simply add wc to the end to count lines to get total number of files:

$ ls | grep -E "[6-9][[:digit:]]_|100_" | wc -l

you can also go with shorter version if the you are sure that first to characters are digits:

ls | grep -E "[6-9]._|100_" | wc -l

Is this what you need? Do not forget to visit our regex tutorial.