Skip to main content

How to get the filename/size without downloading a file in curl

You can do some fun stuff with the --write-out flag and variables.

I was doing some stuff with curl, and I found some uses for the --write-out flag (which Daniel Stenberg has described as “one of [his] personal favorites”).

I was using curl to download snapshots from Wikimedia Commons, and I wanted to know:

curl clearly knows both of these things because it uses them as part of the download process, but I wanted to get them separately so I could write some wrapper scripts around them.

Obviously both of these rely on having a relatively well-behaved web server that doesn’t change filenames and returns a true value in the Content-Length header – which is sufficient for a lot of simple scripting and dealing with trusted downloads.

By default, the --head flag will print a complete list of headers. If you want to suppress this and just print the value of --write-out, you need to add --silent and --output /dev/null, for example:

$ curl --head --silent --output /dev/null --write-out '%header{content-length}' "$url"