Mastering `cURL` in Linux

2 min read .

In the world of Linux, cURL is a powerful command-line tool used for transferring data with URLs. Whether you’re downloading files, interacting with APIs, or performing web requests, cURL offers a versatile solution for managing and retrieving data. In this guide, we’ll explore how to use cURL in Linux, highlighting its key features and practical applications.

What is cURL?

cURL (Client URL) is a command-line tool that enables you to send and receive data using various protocols, including HTTP, HTTPS, FTP, and more. It is widely used for:

  • Downloading Files: Fetch files from the web or FTP servers.
  • Sending HTTP Requests: Interact with APIs and web services.
  • Uploading Files: Upload files to remote servers.

Installing cURL

Most Linux distributions come with cURL pre-installed. To check if it’s installed, run:

curl --version

If cURL is not installed, you can install it using your package manager. For example:

  • On Debian-based systems (Ubuntu, Debian):

    sudo apt-get update
    sudo apt-get install curl

Basic cURL Commands

1. Downloading Files

To download a file from a URL, use the following command:

curl -O http://example.com/file.zip
  • -O: Save the file with the same name as in the URL.

2. Saving Output to a Specific File

To save the downloaded content to a specific file name:

curl -o custom-name.zip http://example.com/file.zip
  • -o: Specify the output file name.

3. Viewing Response Headers

To view the HTTP response headers, use:

curl -I http://example.com
  • -I: Fetch the headers only.

4. Sending Data with POST Requests

To send data using a POST request:

curl -X POST -d "param1=value1&param2=value2" http://example.com/api
  • -X POST: Specify the request method (POST).
  • -d: Include data to be sent.

5. Sending JSON Data

To send JSON data, specify the content type and use -d:

curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' http://example.com/api
  • -H: Set the request header.

Advanced cURL Features

1. Authenticating Requests

To authenticate using Basic Auth:

curl -u username:password http://example.com
  • -u: Provide the username and password.

To use token-based authentication:

curl -H "Authorization: Bearer YOUR_TOKEN" http://example.com

2. Handling Cookies

To save cookies to a file:

curl -c cookies.txt http://example.com
  • -c: Save cookies to a file.

To send cookies from a file:

curl -b cookies.txt http://example.com
  • -b: Send cookies from a file.

3. Following Redirects

To follow redirects automatically:

curl -L http://example.com
  • -L: Follow HTTP redirects.

4. Limiting Download Speed

To limit the download speed:

curl --limit-rate 200K http://example.com/file.zip
  • --limit-rate: Specify the maximum download speed.

5. Resuming Interrupted Downloads

To resume a partially downloaded file:

curl -C - -O http://example.com/file.zip
  • -C -: Resume the download.

Using cURL with Scripts

cURL can be integrated into shell scripts for automated tasks. Here’s a simple example of a script that fetches the status code of a website:

#!/bin/bash
url="http://example.com"
status_code=$(curl -o /dev/null -s -w "%{http_code}" "$url")
echo "Status Code: $status_code"

Save this script as check_status.sh, make it executable, and run it:

chmod +x check_status.sh
./check_status.sh

Conclusion

cURL is an essential tool for managing data transfers and interactions in Linux. By mastering its commands and features, you can efficiently handle file downloads, web requests, and API interactions. Whether you’re a system administrator, developer, or power user, understanding how to leverage cURL will enhance your ability to work with data and automate tasks on your Linux system.

Tags:
Linux

See Also

chevron-up