How to Use the Linux curl Command to Make HTTP Requests and Download Files

When working on a headless Linux server that lacks a graphical user interface, you cannot simply open a web browser to download a file or interact with an application programming interface (API). Instead, system administrators and developers rely on curl (Client URL). The curl command is a phenomenally powerful, versatile tool that allows you to transfer data to or from a remote server using almost every network protocol in existence, including HTTP, HTTPS, FTP, and SCP. Whether you need to quickly download a script from GitHub, test a REST API endpoint, or inspect the HTTP headers sent by a web server, curl is the industry-standard tool for the job.

Downloading Files with curl

The most common use case for a beginner is downloading a file from the internet directly to the server’s hard drive.

If you run curl https://example.com/script.sh, the command will simply fetch the contents of the file and print them directly to your terminal screen. While this is useful for reading a script, it does not save it. To actually download and save the file, you must use the -O (capital letter O, standing for Output) flag.

curl -O https://example.com/script.sh

The -O flag tells curl to save the file in your current directory, using the exact same filename it had on the remote server (in this case, script.sh).

If you want to download the file but save it under a completely different name, you use the lowercase -o flag followed by your desired filename:

curl -o my_new_script.sh https://example.com/script.sh

Testing APIs and Fetching Web Pages

Developers frequently use curl to interact with REST APIs because it allows them to see exactly what raw data a server returns without the interference of a web browser.

To perform a standard GET request to an API endpoint to retrieve JSON data, simply run the command against the URL:

curl https://api.github.com/users/torvalds

The server will respond with the raw JSON string containing the user data for Linus Torvalds, printing it directly to your screen.

If the API requires you to send data (a POST request), you can use the -X flag to define the HTTP method, and the -d (data) flag to send the payload:

curl -X POST -d "name=admin&password=1234" https://api.example.com/login

Inspecting HTTP Headers

When troubleshooting a website—for example, trying to determine if a web server is caching a page or if an SSL certificate is configured correctly—you don’t care about the HTML body of the page; you only want to see the invisible HTTP headers sent by the server.

You can force curl to fetch only the headers using the -I (capital i, for Information/Head) flag.

curl -I https://digitash.com

The output will reveal critical server telemetry, such as the exact HTTP status code (e.g., HTTP/2 200 OK or 301 Moved Permanently), the server software version (e.g., Server: nginx), and the cache-control directives, allowing you to diagnose complex network routing issues instantly.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.