Thursday, August 20, 2009

How Do I Find Missing Files in a Sequence?

In this week's Command-Line Kung Fu, Hal Pomerantz & Ed Skoudis field this question from a reader:

I have about 1300 pictures that I am trying to organize.
They are numbered sequentially eg. ( 0001.jpg -> 1300.jpg )
The problem is that I seem to be missing some...


Is there a fast way to be able to scan the directory to see which ones I am missing? Other than to do it manually, which would take a long time.
While they provide good, loop-based solutions, like this one:
$ for i in $(seq -w 1 1300); do [ ! -f $i.jpg ] && echo $i.jpg; done
and this
for ((i=1; $i <= 1300; i++)); do file=$(printf "%04d.jpg" $i);[ ! -f $file ] && echo $file; done
they don't mention bash brace expansion. In bash 4.0 and above, this does the trick:
$ ls {0001..1300}.jpg >/dev/null
No loop. No subprocess. Quite readable.

5 comments:

Viagra Online said...

Wow 1300 pictures are too much, well at least for me, I think that's the best way to organize it, actually I'm gonna take some tips.

Buy Viagra said...

It'd be the hardest job, isn't? 1300 pictures! I just can't believe it. I'll be able to read your next post and I hope you can't missed anything.

pharmacy said...

That blog is so interesting. I must say that at first I thought it would be a waste of time, but after reading your post was impressed with its quality. I just hope that over time you continue maintaining the same quality, and most of the same passion at the time of writing.

pharmacy said...

I really like this site, it's so important to know more about this topic, keep it up and of course every time I have time I'll love to check out again

tinkthank said...

hey thanks, looks exactly what i would need to do with a directory that has about 90000 dpx images of a film, where some frames are missing. due to the fact that i am really a imbecile when it comes to bash fu: how could i do this for files which are named this way:
vtr_0901159.dpx
vtr_0901160.dpx
vtr_0901161.dpx

etc.? the other question is, as simple commands like mv or rm choke on the amount of files, probably the loop based option still is the better way?

cheers,t.