Skip to content
Advertisement

Using wget to download images and saving with specified filename

I’m using wget mac terminal to download images from a file where each image url is it’s own line, and that works perfectly with this command:

    cut -f1 -d, images.txt | while read url; do wget ${url} -O $(basename ${url}); done

However I want to specify the output filename it’s saved as instead of using the basename. The file name is specified in the next column, separated by either space or comma and I can’t quite figure out how to tell wget to use the 2nd column as the name it should as the -O name.

I’m sure it’s a simple change to my above command but after reading dozens of different posts on here and other sites I can’t figure it out. Any help would be appreciated.

Advertisement

Answer

If you use whitespace as the seperator it’s very easy:

cat images.txt | while read url name; do wget ${url} -O ${name}; done

Explanation: instead of reading just one variable per line (${url}) as in your example, you read two (${url} and ${name}). The second one is your local filename. I assumed your images.txt file looks something like this:

http://cwsmgmt.corsair.com/newscripts/landing-pages/wallpaper/v3/Wallpaper-v3-2560x1440.jpg test.jpg
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement