cURL, a powerful command-line tool for making HTTP requests, is often used by developers and system administrators for fetching content from the web, simulating user interactions, and even debugging applications. In the course of these activities, it's common to retrieve sizable outputs, complex JSON responses, or even binary files.
By default, cURL prints the response of the web request directly to the terminal. However, especially with voluminous responses, it's more practical to save the output to a file for easier analysis, to serve as documentation, or for sharing with colleagues.
Whether you're grabbing a webpage's HTML, fetching an API response, or downloading a file, redirecting the output of a cURL command to a file in your filesystem is straightforward. This guide will walk you through the steps.
$ curl https://www.example.com/ > output.txt
$ curl https://www.example.com/moredata > output.txt
Using » ensures that the previous data in the file remains intact and new data gets appended to it.
$ curl https://invalid.url 2> errorlog.txt
2> directs standard error (often used for error messages) to the designated file.
$ curl https://invalid.url > output.txt 2>&1
Here, 2>&1 tells the shell to redirect the standard error (2) to the same location as the standard output (1).
$ cat output.txt
$ curl -O https://www.example.com/image.jpg
The -O flag tells cURL to save the output in a file with the same name as the remote file. Use -o with a filename to specify a different name.
$ chmod 600 output.txt
It's a best practice to set appropriate permissions on files that hold confidential or sensitive information.
By saving the cURL response to a file, you ensure that the data can be reviewed, processed, or shared without the limitations of viewing it directly in the terminal. Whether you're documenting API responses, archiving content, or analyzing data, this approach offers flexibility and efficiency.
Comment anonymously. Login not required.