Home > Mobile >  how to get file extension from filename with wget?
how to get file extension from filename with wget?

Time:09-13

url = https://website.com/download/media/123

This would maybe return a file called file.zip or maybe not. So I want to check if the file which gets downloaded ends with .zip

Maybe something like this?

wget $url -P ~/downloads
if [ downloaded file ends with .zip ]
then:
    unzip file.zip
else
    echo "Downloaded file doesn't end with zip!"
fi

CodePudding user response:

#!/bin/sh

for a in *
do    
zip=$(printf "${a}" | tail -c 3 | grep -o "zip")

[ -n "${zip}" ] && unzip "${a}"
[ -z "${zip}" ] && echo "File ${a} doesn't end with zip!"
done

CodePudding user response:

Simplifying the idea of get extension and add check via file utility:

for a in *
do    
zip="${a##*.}"
zip2=$(file $a|grep "Zip archive data"|wc -l)
if [ "zip" = "${zip}" ] || [ "$zip2" -gt 0 ]
then unzip "${a}"
else echo "File ${a} is not zip archive"
fi
done
  • Related