1

I only found JavaScript code for doing this through browser console.

For example if I copy image address from the first images from Google images search results grid I get a Data URL.

Commands that I tried and errors:

$ curl 'data:stuff' -o filename 
curl: (3) URL using bad/illegal format or missing URL
$ curl 'data:stuff' > filename
curl: (3) URL using bad/illegal format or missing URL
$ wget 'data:stuff' -O filename
Resolving data (data)... failed: Name or service not known.
wget: unable to resolve host address ‘data’

How can I download Data URL through command line?

0

2 Answers 2

0

The data: scheme is local in the context of a webpage while both wget and curl work with remote data. Perhaps what you're really looking for is base64 -d which allows to decode such data.

Lastly, the data URI scheme can be a lot longer/larger than your shell allows, see: https://stackoverflow.com/a/41755526 and I doubt you want to keep that much random data in your shell history.

0

Adding a bit to the answer of Artem.

You can download xclip through your package manager and use a shell script (You should copy the Data URL):

#! /usr/bin/bash

data=$(xclip -out -selection clipboard)

ext=${data#*/}
ext=${ext%%;*}
ext=${ext/jpeg/jpg}

if [ $1 ]; then
        filename=$1
else
        filename=file
fi

echo "${data#*,}" | base64 -d > $filename.$ext

With no arguments filename is saved as file, otherwise it is your first argument.

Edit: Improved script -- thanks to Freddy's great comment.

2
  • There's no need for a temporary file: echo "${data#*,}" | base64 -d > filename. You could also extract the image type and use it as suffix for the output filename, e.g. ext=${data#*/}; ext=${ext%%;*}; ext=.${ext/jpeg/jpg}. Commented Nov 27, 2020 at 19:52
  • If you’re on macOS, you can replace xclip -out -selection clipboard with pbpaste for the same results. Commented Dec 25, 2022 at 22:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.