{"id":22786,"date":"2023-08-01T08:21:48","date_gmt":"2023-08-01T06:21:48","guid":{"rendered":"https:\/\/daniel.haxx.se\/blog\/?p=22786"},"modified":"2023-08-01T09:45:01","modified_gmt":"2023-08-01T07:45:01","slug":"curl-write-out-to-files","status":"publish","type":"post","link":"https:\/\/daniel.haxx.se\/blog\/2023\/08\/01\/curl-write-out-to-files\/","title":{"rendered":"curl write-out to files"},"content":{"rendered":"\n<p>The curl option <a href=\"https:\/\/curl.se\/docs\/manpage.html#-w\">&#8211;write-out<\/a> is one of my personal favorites and offers users an excitingly powerful  way to output information from a transfer. Over time, it has been extended to provide more and more features.<\/p>\n\n\n\n<p>It was for example not that long ago we added the ability to output the content of specific headers with <a href=\"https:\/\/daniel.haxx.se\/blog\/2022\/03\/24\/easier-header-picking-with-curl\/\">%headers{}<\/a> to this option.<\/p>\n\n\n\n<p>Now (shipping in the coming <strong>curl 8.3.0<\/strong>, merged in commit <a href=\"https:\/\/github.com\/curl\/curl\/commit\/1032f56efae6bdab399d322fea2bd2da0d95365c\">1032f56efa<\/a>) we take the next step and add yet another little nifty function to this option that makes it even more powerful and allows you to use it for more purposes better going forward.<\/p>\n\n\n\n<p>It can now save the selected info to a specific file instead of just outputting to stdout or stderr. Or to multiple files. Or append to files. With <code>%output{}<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples<\/h2>\n\n\n\n<p>Write the used IP address of the remote host to a file named &#8220;remote.txt&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl -w \"%output{remote.txt}%{remote_ip}\" https:\/\/example.com<\/pre>\n\n\n\n<p>Get the same information, but <em>append<\/em> it to the remote.txt file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl -w \"%output{>>remote.txt}%{remote_ip}\" https:\/\/example.com<\/pre>\n\n\n\n<p>Output the HTTP response code from the HTTP server to stderr and then append it to the file &#8220;log.txt&#8221; as well:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl -w \"%{response_code}%output{&gt;&gt;log.txt}%{response_code}\" https:\/\/example.com<\/pre>\n\n\n\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The curl option &#8211;write-out is one of my personal favorites and offers users an excitingly powerful way to output information from a transfer. Over time, it has been extended to provide more and more features. It was for example not that long ago we added the ability to output the content of specific headers with &hellip; <a href=\"https:\/\/daniel.haxx.se\/blog\/2023\/08\/01\/curl-write-out-to-files\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">curl write-out to files<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":5,"featured_media":13469,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[214,33],"class_list":["post-22786","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-curl","tag-command-line","tag-curl-and-libcurl"],"_links":{"self":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/22786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/comments?post=22786"}],"version-history":[{"count":16,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/22786\/revisions"}],"predecessor-version":[{"id":22883,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/22786\/revisions\/22883"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/media\/13469"}],"wp:attachment":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/media?parent=22786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/categories?post=22786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/tags?post=22786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}