curl: named globs in output file name for upload glob references

Use parts of text from the upload filename field when that uses globbing
by giving it a name the same way we do it for URL globs. For example, if
you upload three files to a HTTP URL and want to save the corresponding
responses in separate files:

    curl -T 'file{<num>1,2,3}' https://upload.example/ -o 'response-#<num>'

Verified by test 2014

Closes #21407
This commit is contained in:
Daniel Stenberg 2026-04-22 00:52:16 +02:00
parent 89f38c168c
commit 2238f0921c
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
7 changed files with 145 additions and 22 deletions

View file

@ -23,10 +23,10 @@ Example:
# `--output`
Write output to the given file instead of stdout. If you are using globbing to
fetch multiple documents, you should quote the URL and you can use `#`
followed by a number in the filename. That variable is then replaced with the
current string for the URL being fetched. Like in:
Write output to the given file instead of stdout. If you are using globbing in
the URL to fetch multiple documents, you should quote the URL and you can use
`#` followed by a number in the filename. That variable gets replaced with the
current glob text. Like in:
curl "http://{one,two}.example.com" -o "file_#1.txt"
@ -70,9 +70,9 @@ override curl's internal binary output in terminal prevention:
Note that the binary output may be caused by the response being compressed, in
which case you may want to use the --compressed option.
Starting in curl 8.21.0, the separate globbing parts can be named and
referenced by their names. The case sensitive alphanumeric name is set
enclosed within angle brackets after the opening character. Examples:
Since curl 8.21.0, the separate globbing parts can be named and referenced by
their names. The case sensitive alphanumeric name is set enclosed within angle
brackets after the opening character. Examples:
curl "https://fun.example/{<num>one,two}.jpg" -o "save-#<num>"
@ -80,3 +80,11 @@ enclosed within angle brackets after the opening character. Examples:
-o "save-#<range>.txt"
Referencing a named glob that is not set, causes an error.
Since curl 8.21.0, you can use parts of the upload filename when it uses
globbing by setting a glob name and referencing it the same way you reference
named URL globs. For example, if you upload three files to a single fixed HTTP
URL and want to save the corresponding responses in separate files:
curl -T 'file{<num>1,2,3}' \
https://upload.example/ -o 'response-#<num>'

View file

@ -26,13 +26,13 @@ Upload the specified local file to the remote URL.
If there is no file part in the specified URL, curl appends the local file
name to the end of the URL before the operation starts. You must use a
trailing slash (/) on the last directory to prove to curl that there is no
trailing slash (`/`) on the last directory to prove to curl that there is no
filename or curl thinks that your last directory name is the remote filename
to use.
When putting the local filename at the end of the URL, curl ignores what is on
the left side of any slash (/) or backslash (\\) used in the filename and only
appends what is on the right side of the rightmost such character.
the left side of any slash (`/`) or backslash (`\\`) used in the filename and
only appends what is on the right side of the rightmost such character.
Use the filename `-` (a single dash) to use stdin instead of a given file.
Alternately, the filename `.` (a single period) may be specified instead of
@ -45,9 +45,19 @@ You can specify one --upload-file for each URL on the command line. Each
--upload-file + URL pair specifies what to upload and to where. curl also
supports globbing of the --upload-file argument, meaning that you can upload
multiple files to a single URL by using the same URL globbing style supported
in the URL.
in the URL. Example:
When uploading to an SMTP server: the uploaded data is assumed to be RFC 5322
formatted. It has to feature the necessary set of headers and mail body
formatted correctly by the user as curl does not transcode nor encode it
further in any way.
curl --upload-file 'file{1,2,3}' ftp://ftp.example/
Since curl 8.21.0, you can use parts of the upload filename when it uses
globbing by setting a glob name and referencing that in the same way you
reference named URL globs. For example, if you upload three files to a single
fixed HTTP URL and want to save the corresponding responses in separate files:
curl -T 'file{<num>1,2,3}' \
https://upload.example/ -o 'response-#<num>'
When uploading to an SMTP server (aka "sending email"): the uploaded data is
assumed to be RFC 5322 formatted. It has to feature the necessary set of
headers and mail body formatted correctly by the user as curl does not
transcode nor encode it further in any way.