How to download an image with wget and save it with the md5 hash as name?

5,738

You can do it in different ways. A little script will help. You can call it with /bin/bash myscript.sh http://yourhost/yourimage.ext where_to_save. The destination directory is an optional:

#!/bin/bash
MyLink=${1}
DestDir=${2:-"~/Users/TheGrayFox/Images/"}   # fix destination directory
MyPath=$(dirname $MyLink)                    # strip the dirname  (Not used)
MyFile=$(basename $MyLink)                   # strip the filename
Extension="${MyFile##*.}"                    # strip the extension 

wget $MyLink                                 # get the file
MyMd5=$(md5sum $MyFile | awk '{print $1}')   # calculate md5sum
mv $MyFile  ${DestDir}/${MyMd5}.${Extension} # mv and rename the file
echo $MyMd5                                  # print the md5sum if wanted

The command dirname strips last component from file name, and the command basename strips directory and suffix from filenames.

You can even decide to save directly the file from wget in the destination directory and after to calculate the md5sum and rename it. In this case you need to use wget From_where/what.jpg -O destpath. Note is a capital o O and not a zero.

Share:
5,738

Related videos on Youtube

TheGrayFox
Author by

TheGrayFox

Updated on September 18, 2022

Comments

  • TheGrayFox
    TheGrayFox almost 2 years

    How can I make a download an image, md5 hash the image, and save that image with the md5 hash as name to a directory using wget?

    # An example of the image link...
    http://31.media.tumblr.com/e1b8907c78b46099fd9611c2ab4b69ef/tumblr_n8rul3oJO91txb5tdo1_500.jpg
    
    # Save the image linked with for name the MD5 hash
    
    d494ba8ec8d4500cd28fbcecf38083ba.jpg
    
    # Save the image with the new name to another directory
    
    ~/Users/TheGrayFox/Images/d494ba8ec8d4500cd28fbcecf38083ba.jpg
    
  • TheGrayFox
    TheGrayFox almost 10 years
    Appreciate the explanation, thanks! Some image files contain *.png extensions. On a Mac, md5 is equivalent to our md5sum.