How to save cURL output to a file in Linux? ?

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 URLthe content and save it to output.txta file named. If output.txtit does not exist, it will be created; if it already exists, it will be overwritten.

curl -o output.txt URL

This will download URLthe content and append it to output.txtthe 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 -cthe option to save cookies to a file and then use -bthe option to load cookie information.

curl -c cookies.txt URL

This will save URLthe cookie information fetched from to cookies.txta file named. You can then use -bthe 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 -ithe 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 URLthe content and save the normal output to output.txtand 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 URLthe 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 -woptions, 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 URLthe 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. -OOptions are used for exactly this purpose. With -Ooptions, 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.txtextract the filename from it somefile.txtand 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

-oOption 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, , File2and 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.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132664681