乐闻世界logo
搜索文章和话题

How to download a file using curl

1个答案

1

Basic Usage

If you only need to download a file, you can use the following command:

bash
curl -O [URL]

The -O option instructs curl to save the downloaded file using the filename specified by the server.

Example: Suppose we want to download an image file from https://example.com/sample.jpg, we can use:

bash
curl -O https://example.com/sample.jpg

Specify Filename

If you want to specify a different filename during download, you can use the -o option:

bash
curl -o myfilename.jpg [URL]

Example: Suppose we want to download the same image but save it as newname.jpg:

bash
curl -o newname.jpg https://example.com/sample.jpg

Advanced Usage

Download from Authenticated Sites

If the file you want to download is on a server requiring authentication, curl can handle this. Use the -u option to provide the username and password:

bash
curl -u username:password -O [URL]

Example: Suppose we want to download an authenticated file:

bash
curl -u user123:passwd123 -O https://secure.example.com/protected.zip

Increase Download Speed

If your connection allows, you can attempt to use curl's multi-threaded download options, such as running multiple curl instances to download different parts of the file in parallel. Note that this is not a built-in feature of curl but can be achieved with some scripts.

Summary

curl is a very flexible tool that can be used to download various types of files. Its basic functionality is sufficient for most download needs, while its advanced features can handle more complex scenarios, such as authentication or custom HTTP headers. By combining different options and parameters, you can make curl adapt to almost any download requirement.

2024年6月29日 12:07 回复

你的答案