Introduction
n
In the world of Linux servers and workstations, versatile tools to transfer data are essential. Curl has become one of the most popular utilities thanks to its ability to interact with virtually any network protocol, from HTTP and HTTPS to FTP, SFTP and more. This article will guide you from the fundamental concepts to the advanced tricks that will allow you to make the most of Curl in your day to day.
n
What is Curl?
n
Curl is a command line tool and a library (libcurl) designed to transfer data using various protocols. His name comes from »Client for URLs», which makes clear your 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 accuracy.
n
Installation
n
In most Linux distributions, Curl comes pre-installed. If not available, you can easily add it from your system's package manager. In Debian or Ubuntu-based distributions, runsudo apt-get update && sudo apt-get install curl. In Red Hat, CentOS or Fedora, the command issudo dnf install curl(oryumin old versions). In Arch Linux, usesudo pacman -S curl. After installation, check the version withcurl --version.
n
Basic Command
n
The easiest use of Curl is to make a GET request to a URL and show the result in the standard output. For example:
n
- n
curl https://ejemplo.comdownload the content of the page and print it into the terminal.curl -o archivo.html https://ejemplo.comsave the answer in a file called file.curl -I https://ejemplo.commakes a HEAD request and displays only HTTP headers.curl -v https://ejemplo.comactivates the verbose mode, useful for purifying protocol negotiation.
n
n
n
n
n
These examples illustrate how to combine options to adapt Curl's behavior to your needs.
n
Working with APIs
n
One of the most common applications of Curl today is the interaction with APis REST. Thanks to its ability to specify HTTP methods, headers and request bodies, you can try endpoints quickly and reproducible. For example, to send a POST application with JSON data:
n
curl -X POST https://api.ejemplo.com/v1/recursos \\\n -H \"Content-Type: application/json\" \\\n -d '{\"nombre\":\"Juan\",\"edad\":30}'\n
n
The modifier-Xdefines the method,-Hadds headers and-dsends the body of the request. Similarly, you can authenticate with tokens Bearer using-H \"Authorization: Bearer TU_TOKEN\"or with basic authentication by-u usuario:contraseña.
n
Automation with scripts
n
Curl is seamlessly integrated into shell scripts, which allows you to automate tasks such as backup, service health verification or data synchronization between systems. Here are some useful patterns:
n
- n
- Download a file and check its integrity with a SHA256 hash:
n
curl -sSf https://ejemplo.com/archivo.tar.gz -o archivo.tar.gz\nif [[ $(sha256sum archivo.tar.gz | cut -d' ' -f1) == \"expected_hash\" ]]; then\n echo \"Descarga correcta\"\nelse\n echo \"Error de integridad\"\nfi\n
n
n
for i in {1..5}; do\n if curl -sSf https://api.ejemplo.com/estado; then\n break\n fi\n sleep $((2**i))\ndone\n
n
n
These fragments show how to combine Curl with bash constructions to create robust workflows.
n
Tricks and good practices
n
To make the most of Curl, take into account the following tips:
n
- n
- Use
-sSfor silent mode but to show errors, ideal in scripts. - Limit the download speed with
--limit-ratenot to saturate the bandwidth. - Enable redirection tracking with
-Lwhen the URL can change. - Save cookies between requests using
-c cookies.txtfor reading and-b cookies.txtfor shipping, useful in authentication flows. - Reuse connections by
--keepalive-timeand--max-timeto control timeouts.
n
n
n
n
n
n
Applying these practices will improve the reliability and performance of your network operations.
n
Conclusion
n
Curl is an indispensable tool for any professional working with Linux. Its versatility allows from simple file downloads to complex interactions with APIs and critical task automation. Dominating your options and combining it with shell scripts gives you complete control over network communication in your systems. We invite you to experiment with the examples presented and to adapt them to your own workflows.


