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 File
- Download and Save with a Custom Name
- Follow Redirects
- Download Multiple Files
- Resume an Interrupted Download
- Send a POST Request with Data
- Send a POST Request with JSON Data
- Send a GET Request with Headers
- Download Files via FTP
- Upload a File via FTP
- Limit Download Speed
- Display Response Headers Only
- Save Cookies to a File
- Use Cookies from a File
- Send a PUT Request
- Send a DELETE Request
- Download a File with Authentication
- Download 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.zipto 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:
curlsupports HTTP, HTTPS, FTP, FTPS, SCP, SFTP, and more. - Data Transfer: Use
-dfor sending data and-Tfor uploading files. - Authentication: Use
-ufor basic authentication or-Hfor token-based authentication.
