Command Line Downloads: Getting Started with cURL – wiki基地

Here’s an article detailing “Command Line Downloads: Getting Started with cURL”:


Command Line Downloads: Getting Started with cURL

In the world of command-line utilities, cURL (Client URL) stands out as an indispensable tool for transferring data with URLs. Whether you’re a developer fetching API data, a system administrator downloading installation packages, or just someone who prefers the efficiency of the terminal, cURL is your go-to companion for command-line downloads.

This article will guide you through the essentials of cURL, from basic file downloads to more advanced usage, helping you master this powerful utility.

What is cURL? Why Use It?

cURL is a command-line tool and library for transferring data with URLs. It supports a vast range of protocols, including HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP, FILE, and more.

Why use cURL for downloads?

  1. Automation: Ideal for scripting and automating download tasks without manual intervention.
  2. Efficiency: No graphical overhead; runs entirely in the terminal, consuming minimal resources.
  3. Versatility: Supports almost every network protocol you’ll encounter.
  4. Debugging: Excellent for inspecting HTTP headers, response codes, and network interactions.
  5. Remote Access: Works seamlessly over SSH connections to remote servers.

Installation

cURL is pre-installed on most Unix-like systems (Linux, macOS).

  • Linux (Debian/Ubuntu):
    bash
    sudo apt update
    sudo apt install curl
  • Linux (Fedora/RHEL/CentOS):
    bash
    sudo dnf install curl
  • macOS: cURL comes pre-installed.
  • Windows: Modern versions of Windows 10/11 often include cURL by default. You can check by opening Command Prompt or PowerShell and typing curl --version. If not present, you can download it from the official cURL website or via package managers like Chocolatey:
    bash
    choco install curl

Basic Downloads

The simplest way to use cURL is to provide a URL. By default, cURL prints the content of the URL to standard output (your terminal screen).

bash
curl https://example.com

This command will display the HTML content of example.com directly in your terminal.

Saving to a File

Usually, you’ll want to save the downloaded content to a file. cURL offers a few ways to do this.

1. Specifying Output Filename (-o or --output)

Use the -o (lowercase ‘o’) flag to specify the exact filename for the downloaded content.

bash
curl -o webpage.html https://example.com

This will download the content of https://example.com and save it as webpage.html in your current directory.

2. Saving with Remote Filename (-O or --remote-name)

If you want cURL to use the filename suggested by the remote server (often derived from the URL itself), use the -O (uppercase ‘O’) flag.

bash
curl -O https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz

This will download the Python tarball and save it as Python-3.10.0.tgz in your current directory.

Downloading Multiple Files

You can download multiple files in a single cURL command by providing multiple -O flags followed by their respective URLs.

bash
curl -O https://example.com/file1.zip -O https://example.com/file2.zip

This will download both file1.zip and file2.zip to your current directory, using their remote names.

Resuming Downloads

Network issues happen. If a download is interrupted, cURL can resume it from where it left off, saving you time and bandwidth. Use the -C - (capital ‘C’ followed by a hyphen) flag for this.

bash
curl -C - -O https://example.com/large_file.iso

If large_file.iso partially exists in your current directory, cURL will detect its size and continue the download from that point.

Authentication

Many resources require authentication. cURL supports various authentication methods.

Basic HTTP Authentication (-u or --user)

For websites requiring a username and password, use the -u flag.

bash
curl -u username:password https://api.example.com/data

cURL will send the credentials encoded in the HTTP Authorization header. For security, you can omit the password, and cURL will prompt you for it interactively.

bash
curl -u username https://api.example.com/data

HTTPS/SSL Considerations

cURL handles HTTPS automatically by default, verifying the server’s SSL certificate. If you’re working with a server that has a self-signed or invalid certificate (e.g., in a development environment), you might encounter an error. To bypass SSL certificate verification (use with caution, only when you understand the risks!):

bash
curl -k https://insecure.example.com

The -k or --insecure flag tells cURL to proceed without verifying the peer’s certificate.

Other Useful Options

  • Show progress bar (-# or --progress-bar): Displays a simple progress bar during large downloads.
    bash
    curl -# -O https://example.com/huge_archive.zip
  • Follow redirects (-L or --location): If the URL redirects, cURL will follow the redirect.
    bash
    curl -L https://shorturl.at/abcde
  • Include headers in output (-i or --include): Shows HTTP response headers along with the body.
    bash
    curl -i https://example.com
  • Send custom headers (-H or --header): Useful for API interactions (e.g., setting User-Agent or Content-Type).
    bash
    curl -H "User-Agent: MyCustomAgent/1.0" https://example.com
  • Send POST data (-X POST -d): For sending data to a server, typically with APIs.
    bash
    curl -X POST -d "param1=value1&param2=value2" https://api.example.com/submit

    Or, send JSON data:
    bash
    curl -X POST -H "Content-Type: application/json" -d '{"key": "value"}' https://api.example.com/data

Conclusion

cURL is a Swiss Army knife for network operations from the command line. Its flexibility, broad protocol support, and robust features make it an essential tool for anyone interacting with network resources. By mastering the basic and intermediate options discussed here, you’ll significantly enhance your ability to download, transfer, and interact with data programmatically, boosting your productivity and control in the terminal.


滚动至顶部