Master Curl in Linux: From the basics to advanced tricks

Introduction

In the world of Linux servers and workstations, having versatile tools for transferring data is essential. Curl has become one of the most popular utilities thanks to its ability to interact with practically any network protocol, from HTTP and HTTPS to FTP, SFTP, and more. This article will guide you from fundamental concepts to advanced tricks that will allow you to get the most out of Curl in your day-to-day work.

What is Curl?

Curl is a command-line tool and a library (libcurl) designed to transfer data using various protocols. Its name comes from “Client for URLs”, which makes clear its focus on working with web addresses. Unlike other utilities such as wget, Curl is not limited to downloading files; it can send data, authenticate, handle custom headers, and perform API tests with great precision.

Installation

In most Linux distributions, Curl comes preinstalled. If it is not available, you can easily add it from your system’s package manager. On Debian- or Ubuntu-based distributions, run sudo apt-get update && sudo apt-get install curl. On Red Hat, CentOS, or Fedora, the command is sudo dnf install curl (or yum on older versions). On Arch Linux, use sudo pacman -S curl. After installation, verify the version with curl --version.

Basic Commands

The simplest use of Curl consists of making a GET request to a URL and displaying the result on standard output. For example:

  • curl https://example.com downloads the page content and prints it in the terminal.
  • curl -o file.html https://example.com saves the response to a file named file.html.
  • curl -I https://example.com performs a HEAD request and shows only the HTTP headers.
  • curl -v https://example.com enables verbose mode, useful for debugging protocol negotiation.

These examples illustrate how to combine options to adapt Curl’s behavior to your needs.

Working with APIs

One of the most common applications of Curl today is interacting with REST APIs. Thanks to its ability to specify HTTP methods, headers, and request bodies, you can test endpoints quickly and reproducibly. For example, to send a POST request with JSON data:

curl -X POST https://api.example.com/v1/resources \
     -H "Content-Type: application/json" \
     -d '{"name":"John","age":30}'

The -X modifier defines the method, -H adds headers, and -d sends the request body. Likewise, you can authenticate with Bearer tokens using -H "Authorization: Bearer YOUR_TOKEN" or with basic authentication via -u username:password.

Automation with Scripts

Curl integrates seamlessly into shell scripts, allowing you to automate tasks such as downloading backups, checking service health, or synchronizing data between systems. Below are some useful patterns:

  • Download a file and verify its integrity with a SHA256 hash:
  • curl -sSf https://example.com/file.tar.gz -o file.tar.gz
    if [[ $(sha256sum file.tar.gz | cut -d' ' -f1) == "expected_hash" ]]; then
        echo "Download successful"
    else
        echo "Integrity error"
    fi
    
  • Retry a failed request up to a maximum number of attempts:
  • for i in {1..5}; do
        if curl -sSf https://api.example.com/status; then
            break
        fi
        sleep $((2**i))
    done
    

These snippets show how to combine Curl with bash constructs to create robust workflows.

Tips and Best Practices

To get the most out of Curl, keep the following tips in mind:

  • Use -sS for silent mode but show errors, ideal in scripts.
  • Limit download speed with --limit-rate to avoid saturating bandwidth.
  • Enable redirect following with