piotrek
piotrek

Reputation: 14530

curl: how to encode file name?

When I run

curl  -J -O --location-trusted "http:..."

it correctly saves a file but the file name (the one from response) looks like 11%20-%204%20-%20..... how can i tell curl to decode it and save that name with with spaces, brackets etc?

Upvotes: 1

Views: 1089

Answers (1)

David W.
David W.

Reputation: 107040

Answer: You can't. At least not directly.

The -O (Capital "O") tells curl to name the file after the URL's final part after the last forward slash. If that contains encoded characters, your file name will have encoded characters. As it says in the manpage:

There is no URL decoding done on the file name. If it has %20 or other URL encoded parts of the name, they will end up as-is as file name.

However, you can use the -o (Lowercase "o") option to give your file a proper name. You can use sed to remove encodings before you name the file:

URL=.....
file_name=$(sed 's/%20/ /g' <<<${URL##*/})
curl -o "$file_name" $URL

Note: There may be other percent encoded characters in the file's name although %20 to space gets the vast majority of them.

Upvotes: 2

Related Questions