Xiang Chen
Xiang Chen

Reputation: 41

How to use curl get image right filename and extension

My links just like below

https://cdn.sspai.com/2022/06/22/article/a88df95f3401d5b6c9d716bf31eeef33?imageView2/2/w/1120/q/90/interlace/1/ignore-error/1

If I use chrome to open this like and cmd + s I will get the right filename and right extension png.

But if I use bash below, then it will no extension:

curl -J -O https://cdn.sspai.com/2022/06/22/article/a88df95f3401d5b6c9d716bf31eeef33?imageView2/2/w/1120/q/90/interlace/1/ignore-error/1

I just want to download image with right filename and extension.

a88df95f3401d5b6c9d716bf31eeef33.png

Same error include different image links below:

https://cdn.sspai.com/article/fa848601-4cdf-38b0-b020-7afd6efc4a7e.jpg?imageMogr2/auto-orient/quality/95/thumbnail/!800x400r/gravity/Center/crop/800x400/interlace/1

Upvotes: 0

Views: 250

Answers (1)

Harkaitz
Harkaitz

Reputation: 66

You can get the name from the URL itself.

url="YOUR-URL"
file="`echo "${url}" | sed 's|\?.*|.jpg|' | xargs basename`"
curl -o "${file}.tmp" "${url}"
mv "${file}.tmp" "${file}"

Hope it helps

Upvotes: 1

Related Questions