Blog / Linux/ Practical cURL Command Examples for Linux

Practical cURL Command Examples for Linux

linux curl命令用法示例二

1. Save Page Content to a File

Use the -o option to save URL content to a specified file.

curl -o home.html http://www.baidu.com

This command saves the Baidu homepage content to home.html in the current directory.

For batch downloads with custom filenames, you can use the # placeholder. For example:

curl -o #2_#1.jpg http://cgi2.tky.3web.ne.jp/~{A,B}/[001-201].JPG

This downloads JPG files numbered 001 to 201 from two directories (A and B). Files are named as sequence-directory.JPG (e.g., 001-A.JPG), avoiding name conflicts.

2. Download Files with -O

Use -O (uppercase O) to save content to the current directory using the filename from the URL. The URL must point to a specific file.

curl -O http://www.baidu.com/img/bdlogo.gif

This creates bdlogo.gif in the current directory.

Supports batch downloads with patterns:

curl -O http://example.com/screen[1-10].JPG

This downloads ten files from screen1.jpg to screen10.jpg.

3. Simulate Login & Cookie Management

Save Cookies to a File

Use -c (lowercase) to save server cookies to a file.

curl -c ./cookie_c.txt -F log=username -F pwd=password http://example.com/wp-login.php

Save HTTP Headers to a File

Use -D to save the full HTTP response headers (including Set-Cookie) to a file.

curl -D ./cookie_D.txt -F log=username -F pwd=password http://example.com/wp-login.php

Note: -c saves parsed cookie data, while -D saves raw HTTP headers; formats differ.

Use Cookie File for Access

Use -b to read a saved cookie file to maintain login state.

curl -b ./cookie_c.txt http://example.com/wp-admin

4. Resume Interrupted Download

Use -C - to resume a partial download from where it left off.

curl -C - -O http://www.baidu.com/img/bdlogo.gif

5. Send POST Data

Use -d to send POST request data, often for form submissions.

curl -d "log=username" http://example.com/wp-login.php

The server response is returned, useful for verifying submission.

6. Show Error Information

With -f, if the server returns an HTTP error (e.g., 404, 500), curl exits with a non-zero status and shows a concise error.

curl -f http://example.com/asdf
# Example output: curl: (22) The requested URL returned error: 404

Without -f, curl outputs the error page HTML.

7. Forge Referer

Use -e or --referer to set the HTTP Referer header, bypassing some hotlink checks.

curl -e http://localhost http://example.com/wp-login.php

8. Use a Proxy

If your IP is restricted, use -x to route through a proxy server.

curl -x 192.168.1.1:8080 -o home.html http://example.com

9. Download File in Ranges

Use -r to specify byte ranges for segmented or parallel downloads.

# Download bytes 0-100
curl -r 0-100 -o img.part1 http://example.com/largefile.jpg
# Download bytes 100-200
curl -r 100-200 -o img.part2 http://example.com/largefile.jpg
# Download from byte 200 to end
curl -r 200- -o img.part3 http://example.com/largefile.jpg

After downloading, merge parts with cat:

cat img.part* > complete_img.jpg

10. Control Output Information

Silent Mode

Use -s to hide progress and error messages.

curl -s -o logo.jpg http://www.baidu.com/img/bdlogo.gif

Show Progress Bar

Use -0 (zero) to force HTTP/1.0 and display a simple progress bar.

curl -0 http://www.baidu.com/img/bdlogo.gif

11. FTP Operations

FTP Download

Use -u to specify credentials for FTP download.

curl -u username:password -O ftp://example.com/demo/style.css
# Or embed credentials in URL
curl -O "ftp://username:[email protected]/demo/style.css"

FTP Upload

Use -T to upload a file to an FTP server.

curl -T localfile.sql "ftp://username:[email protected]/demo/"

12. Set HTTP Headers

Simulate Browser User-Agent

Use -A to set the User-Agent string.

curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" -o page.html http://www.baidu.com

13. HTTP Methods: GET, POST & PUT

GET Request

GET parameters are typically appended to the URL.

curl "http://example.com/login.cgi?user=nick&password=12345"

POST Request

Use -d to send POST request body data.

curl -d "user=nick&password=12345" http://example.com/login.cgi

PUT Request

Use -T to send a PUT request, often for file upload.

curl -T localfile http://example.com/upload.cgi

File Upload (multipart/form-data)

To simulate a form with file upload, use -F.

curl -F "upload=@localfile" -F "nick=go" http://example.com/up_file.cgi

@localfile uploads the file named localfile; the field name upload must match the form definition.

Post a Comment

Your email will not be published. Required fields are marked with *.