Reputation: 123
I'm trying to download a list of .jpg files using wget. The file names on the server are all random alphanumeric strings; I want to save them to new, numbered file names, e.g. 000.jpg, 001.jpg, etc. based on the order that they're listed in the input file.
The problem I'm having is that BASH strips out the leading zeros, so I get 000.jpg, 1.jpg, etc.
Here's my script:
picnum=000
for i in `cat $imglist`; do
wget -O $picnum.jpg $i
let picnum++
done
All of the techniques that I've been able to find for preserving leading zeros look like they wouldn't work in this situation, so I'd really appreciate any assistance.
Upvotes: 2
Views: 1301
Reputation: 34994
You should not use for i in cat
. For your example, it may work, but you should use read from within a while loop to iterate over the lines in a file. See this link for more information: http://mywiki.wooledge.org/BashFAQ/001.
You can use printf to pad your number with 0s.
picnum=0
while read -r image; do
wget -O $(printf '%03d.jpg' "$picnum") "$image"
(( picnum++ ))
done < "$imglist"
Upvotes: 1
Reputation: 41
You need to zero-pad your number. Try this:
wget -O `printf "%03d" $picnum`.jpg $i
Upvotes: 3