The curl
command in Linux is a versatile tool for transferring data to or from a server. It supports a wide range of protocols, including HTTP, HTTPS, FTP, and more. Below are practical examples to master curl
:
Contents
Download a FileDownload and Save with a Custom NameFollow RedirectsDownload Multiple FilesResume an Interrupted DownloadSend a POST Request with DataSend a POST Request with JSON DataSend a GET Request with HeadersDownload Files via FTPUpload a File via FTPLimit Download SpeedDisplay Response Headers OnlySave Cookies to a FileUse Cookies from a FileSend a PUT RequestSend a DELETE RequestDownload a File with AuthenticationDownload a File with a Progress Bar
Download a File
curl -O https://example.com/file.zip
-O
: Saves the file with its original name.
Download and Save with a Custom Name
curl -o custom_name.zip https://example.com/file.zip
-o
: Saves the file ascustom_name.zip
.
Follow Redirects
curl -L https://example.com
-L
: Follows HTTP redirects.
Download Multiple Files
curl -O https://example.com/file1.zip -O https://example.com/file2.zip
Downloads multiple files in sequence.
Resume an Interrupted Download
curl -C - -O https://example.com/large_file.iso
-C -
: Resumes a partially downloaded file.
Send a POST Request with Data
curl -X POST -d "param1=value1¶m2=value2" https://example.com/api
-X POST
: Specifies the HTTP method.-d
: Sends data in the request body.
Send a POST Request with JSON Data
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://example.com/api
-H
: Adds a header (Content-Type: application/json
).-d
: Sends JSON data.
Send a GET Request with Headers
curl -H "Authorization: Bearer token" https://example.com/api
-H
: Adds a custom header (Authorization: Bearer token
).
Download Files via FTP
curl -u username:password -O ftp://example.com/file.zip
-u
: Authenticates with the FTP server.
Upload a File via FTP
curl -u username:password -T file.zip ftp://example.com/
-T
: Uploadsfile.zip
to the FTP server.
Limit Download Speed
curl --limit-rate 200K -O https://example.com/large_file.iso
--limit-rate 200K
: Limits the download speed to 200 KB/s.
Display Response Headers Only
curl -I https://example.com
-I
: Fetches only the HTTP headers.
Save Cookies to a File
curl -c cookies.txt https://example.com
-c
: Saves cookies tocookies.txt
.
Use Cookies from a File
curl -b cookies.txt https://example.com
-b
: Sends cookies fromcookies.txt
.
Send a PUT Request
curl -X PUT -d "param1=value1" https://example.com/api
-X PUT
: Specifies the HTTP method.
Send a DELETE Request
curl -X DELETE https://example.com/api/resource
-X DELETE
: Sends a DELETE request.
Download a File with Authentication
curl -u username:password -O https://example.com/file.zip
-u
: Authenticates with the server.
Download a File with a Progress Bar
curl -# -O https://example.com/large_file.iso
-#
: Displays a progress bar during the download.
Key Notes:
- Protocols:
curl
supports HTTP, HTTPS, FTP, FTPS, SCP, SFTP, and more. - Data Transfer: Use
-d
for sending data and-T
for uploading files. - Authentication: Use
-u
for basic authentication or-H
for token-based authentication.