Here is what I got with my binary search comment.
It does not need memory and takes only 0.003 seconds and 35 files with 100,000 files.
I think you could code it in PHP or put it on it.
#!/bin/bash checkfile(){ if [ -f "$1.jpg" ]; then echo DEBUG: Testing ${i}.jpg, exists - so move min marker to $1 min=$1 return 0 else echo DEBUG: Testing ${i}.jpg, nope - so move max marker to $1 max=$1 return 1 fi } i=1 min=0 max=-1 while : ; do if checkfile $i && [[ $max -eq -1 ]]; then ((i*=2)) else ((i=(max+min)/2)) fi diff=$((max-min)) [[ $diff -eq 1 ]] && break done echo Result:$min
Output:
DEBUG: Testing 1.jpg, exists - so move min marker to 1 DEBUG: Testing 2.jpg, exists - so move min marker to 2 DEBUG: Testing 4.jpg, exists - so move min marker to 4 DEBUG: Testing 8.jpg, exists - so move min marker to 8 DEBUG: Testing 16.jpg, exists - so move min marker to 16 DEBUG: Testing 32.jpg, exists - so move min marker to 32 DEBUG: Testing 64.jpg, exists - so move min marker to 64 DEBUG: Testing 128.jpg, exists - so move min marker to 128 DEBUG: Testing 256.jpg, exists - so move min marker to 256 DEBUG: Testing 512.jpg, exists - so move min marker to 512 DEBUG: Testing 1024.jpg, exists - so move min marker to 1024 DEBUG: Testing 2048.jpg, exists - so move min marker to 2048 DEBUG: Testing 4096.jpg, exists - so move min marker to 4096 DEBUG: Testing 8192.jpg, exists - so move min marker to 8192 DEBUG: Testing 16384.jpg, exists - so move min marker to 16384 DEBUG: Testing 32768.jpg, exists - so move min marker to 32768 DEBUG: Testing 65536.jpg, exists - so move min marker to 65536 DEBUG: Testing 131072.jpg, nope - so move max marker to 131072 DEBUG: Testing 98304.jpg, exists - so move min marker to 98304 DEBUG: Testing 114688.jpg, nope - so move max marker to 114688 DEBUG: Testing 106496.jpg, nope - so move max marker to 106496 DEBUG: Testing 102400.jpg, nope - so move max marker to 102400 DEBUG: Testing 100352.jpg, nope - so move max marker to 100352 DEBUG: Testing 99328.jpg, exists - so move min marker to 99328 DEBUG: Testing 99840.jpg, exists - so move min marker to 99840 DEBUG: Testing 100096.jpg, nope - so move max marker to 100096 DEBUG: Testing 99968.jpg, exists - so move min marker to 99968 DEBUG: Testing 100032.jpg, nope - so move max marker to 100032 DEBUG: Testing 100000.jpg, exists - so move min marker to 100000 DEBUG: Testing 100016.jpg, nope - so move max marker to 100016 DEBUG: Testing 100008.jpg, nope - so move max marker to 100008 DEBUG: Testing 100004.jpg, nope - so move max marker to 100004 DEBUG: Testing 100002.jpg, nope - so move max marker to 100002 DEBUG: Testing 100001.jpg, nope - so move max marker to 100001 Result:100000