Skip to content
shellhacks.dev
APIs

Save a download with its remote name

Save a download under the name in the URL with curl -O. It writes to the current directory, and a URL that ends in a slash has no name to use.

1 min read
Summarize in AI

A download should land in a file called what the URL says, without typing the name twice.

Shell
mkdir dl && cd dl
curl -sO http://127.0.0.1:8000/report.txt
ls
curl -sO http://127.0.0.1:8000/
echo "exit: $?"
Text
report.txt
exit: 23

-O (--remote-name) takes the last path segment of the URL as the file name and saves into the current directory. The option is in the curl manual. When the URL ends in a slash there is no segment, and curl refuses to guess, and the last command exits with status 23 instead of saving anything.

The name comes from the URL, not from the server, so a redirect to a different file name still saves under the first one. Add -J if you want the name from the Content-Disposition header, and keep in mind that -O overwrites an existing file of the same name without asking.

Share

More snippets

APIs

Show only the response headers

Show only the response headers with curl, either by sending a HEAD request or by dumping the headers of a real GET with -D - and dropping the body.

1 min read
APIs

Send a custom header

Send a custom request header with curl -H 'Name: value'. A header with an empty value after a colon removes one of curl's defaults, such as User-Agent.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.