Today I encountered a “probleem” that I needed to download a set of 125 images, all having an incrementing number . Because I’m too lazy to save 125 images by hand, I made a “quick and dirty” bash script to achieve just this. It can be used to download any successively numbered files.
#!/bin/bash for (( x=1; x<=125; x++ )) do if [ $x -lt 10 ]; then PFX="000" elif [ $x -lt 100 ]; then PFX="00" elif [ $x -lt 1000 ]; then PFX="0" else PFX="" fi wget "http://somesite.com/galleries/SomeGallery/gallery_image_$PFX$x.jpg" done
We start out with a for-loop with variable $x that goes from 1 to 125. Next, we check if the value of $x is smaller than 10, 100 of 1000. If that is the case, we set a value to PFX, which I used as a shortcut for “prefix” and is used to generate any “leading zero’s” to get strings like 0001 and 0027, rather than just 1 or 27, what would result in 404 errors. Finally, we sent the URL to wget, using the proper PFX and $x values. Now we have all 125 images in a matter of seconds, rather than manually saving them all by hand.