When you need to save the output of cURL to a file, Linux provides several different methods. cURL is a powerful command line tool for transferring data over the network, typically for HTTP requests. In this article, we'll explore how to use cURL to save its output to a file, along with some additional options and tricks.
Install cURL
Before describing how to use cURL to save output to a file, it is critical to ensure that cURL is installed. Here is a guide on how to install cURL, depending on the Linux distribution you are using:
Ubuntu/Debian
sudo apt install curl
Fedora/RHEL
sudo dnf install curl
Arch Linux
sudo pacman -S curl
Use redirection operator
The simplest way is to use the redirection operator >
or >>
to save the output of cURL to a file. >
The file contents will be overwritten and >>
appended to the end of the file.
curl -o output.txt URL
This will download URL
the content and save it to output.txt
a file named. If output.txt
it does not exist, it will be created; if it already exists, it will be overwritten.
curl -o output.txt URL
This will download URL
the content and append it to output.txt
the end of the file named.
For example, we will access Baidu’s domain name:
curl -o output.txt https://www.baidu.com
Use -c option to save cookies
Sometimes, you may need to save cookie information for cURL requests. You can use -c
the option to save cookies to a file and then use -b
the option to load cookie information.
curl -c cookies.txt URL
This will save URL
the cookie information fetched from to cookies.txt
a file named. You can then use -b
the option to load cookie information:
curl -b cookies.txt URL
For example, we will access Baidu’s domain name:
curl -b cookies.txt https://www.baidu.com
Save HTTP header information
If you want to save HTTP response headers, you can use -i
the option to save them to a file:
curl -i -o output.txt URL
This will save the HTTP response header information to output.txt
.
For example, we will access Baidu’s domain name:
curl -i -o output.txt https://www.baidu.com
Save both output and error messages
Sometimes, you may want to save cURL output and error messages to separate files. You can use 2>
the operator to redirect error messages to a file:
curl -o output.txt URL 2> error.txt
This will download URL
the content and save the normal output to output.txt
and error messages to error.txt
.
Save to specific directory
If you want to save the file to a specific directory, you can include the directory path in the file name:
curl -o /path/to/directory/output.txt URL
This will download URL
the content and save it to a file /path/to/directory/
in the directory output.txt
.
For example, we will access Baidu's domain name and save the results to /tmp/test/baidu/output.txt:
curl -o /tmp/test/baidu/output.txt https://www.baidu.com
Note: Before saving to a specific directory, be sure to ensure that the directory exists first!
Use the -w option to customize the output format
Using -w
options, you can customize cURL's output format. For example, you could just save the response's HTTP status code:
curl -o output.txt -w "%{http_code}" URL
This will download URL
the content and save the HTTP status code to output.txt
.
For example, we will access Baidu’s domain name:
curl -o output.txt -w "%{http_code}" https://www.baidu.com
Save file with original file name
In some cases, it is more convenient to use the original file name when saving the file. -O
Options are used for exactly this purpose. With -O
options, cURL will extract the original filename from the URL and save the file in the current working directory with the same filename as the original filename. Here is an example usage:
curl http://example.com/somefile.txt -O
In this example, cURL will http://example.com/somefile.txt
extract the filename from it somefile.txt
and save it in the current working directory with the same filename as the original filename. This is very convenient for situations where the original file name needs to be preserved.
For example, we will access this address https://www.wljslmz.cn/20045.html
:
Save this article and name it 20045.html:
curl https://www.wljslmz.cn/20045.html -O
Save multiple files using cURL command
-o
Option can be used to specify an output file name for each link, which is very convenient for downloading files in batches. Here is an example usage:
curl https://link-1.com https://link-2.com https://link-3.com -o File1 -o File2 -o File3
In this example, cURL will download files from three different links and save them as File1
, , File2
and File3
.
This is useful for downloading multiple files, especially when you need to automate download tasks.
For example:
curl https://www.baidu.com https://www.baidu.com https://www.baidu.com -o File1 -o File2 -o File3
Summarize
These are some common methods and tricks to save output to file using cURL in Linux. You can choose the most suitable method according to your needs. Hope this article helps you! If you have any questions or need more information, please feel free to ask.