简体   繁体   中英

how to get file extension from filename with wget?

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
#!/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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM