Curl
curl(1) is a command-line tool for fetching internet (chiefly web or FTP) content.
The project (which encompasses both curl(1) and libcurl) is stylized as cURL, and stands for "Client URL".
Contents
Installation
All Linux and BSD distributions offer a curl package.
Usage
curl -output path/to/file URL
If the output option is not specified, the response will be written to STDOUT.
Tips
Suppressing Progress Bar
curl --no-progress-meter https://example.com
Downloading a File
curl --output path/to/file --fail https://example.com
In the case of a server error (HTTP 4xx), an error page is often returned by the server. The --fail option suppresses this page from output and causes curl(1) to exit with code 22.
Checking Availability
code=$(curl --silent --fail --head --write-out '%{http_code}' https://example.com) case $code in 0) echo "Success! Server returned HTTP code ${code}" ;; 22) echo "Error! Server returned HTTP code ${code}" ;; 6) echo "Error! Server not found" ;; 60) echo "Error! SSL/TLS certificate cannot be verified" ;; *) echo "Error!" ;; esac
The --head option causes curl(1) to only fetch only headers. The --silent option suppresses all output, including the progress bar or any returned pages.
The --write-out option takes a format string that will be populated with available variables.
Other useful variables:
Name |
Contains |
content_type |
Content type of the document |
http_version |
Version of HTTP that was used |
size_header |
Number of bytes in downloaded headers |
size_download |
Number of bytes in downloaded body and data (i.e. not headers) |
time_namelookup |
Number of seconds from start until name was resolved |
time_connect |
Number of seconds from start until TCP connection |
time_appconnect |
Number of seconds from start until SSL handshake |
time_pretransfer |
Number of seconds from start until all pre-transfer steps are complete |
time_starttransfer |
Number of seconds from start until first transferred byte is received |
time_total |
Number of seconds |
speed_download |
Average number of bytes per second |
ssl_verify_result |
Result of certificate verification (0 if successful) |