A command-line utility that allows you to transfer data using various network protocols, with which you can interact with websites, APIs and even communicate with different devices on your network – all from the comfort of your terminal window. That’s what the curl command can do for you. Isn’t it amazing?
If you have recently stumbled upon it or have been scratching your head over it for a while, this article is just for you. We are set to delve into the curl command in Linux, with a particular emphasis on guiding beginners. We will uncover its nature and learn the methods to utilize it to its full potential.
What is Curl?
Curl is an acronym for Client URL. It is a tool that can be used from the command line to transfer data between your machine and a network server, employing any of the protocols it supports. These protocols include HTTP, HTTPS, FTP, FTPS, SCP, SFTP, DICT, and others. Basically, if there’s a common protocol, Curl probably supports it!
Curl is especially popular for working with HTTP and HTTPS – which are the protocols web browsers use to access web pages. However, unlike a web browser, Curl doesn’t have a graphical user interface. Everything is done through the command line, which makes it very powerful for automation scripts and testing.
Why Use Curl?
- Simplicity and Flexibility: Curl is simple to use. Even with just the curl command followed by a URL, you can recover information. It also has a multitude of options that cater to various requirements, making it highly flexible.
- Support for Multiple Protocols: As mentioned, Curl supports a wide array of protocols. This means that you can use it not just for web pages but for interacting with FTP servers, sending emails (SMTP), and much more.
- Data Transfer: You can use Curl to download and upload files. Whether it’s a single file or content from an entire web page, Curl has got you covered.
Installing Curl
Now that we understand what Curl is and why it’s an essential tool for handling web data, it’s time to get it installed on our Linux systems. Curl is widely supported and can be installed on various Linux distributions.
On Debian-based Systems
If you are operating on a Debian-based Linux distribution, like Ubuntu or Debian itself, the curl installation can be done using the apt-get command. Install curl by executing:
On Red Hat-based Systems
In case you are using a Red Hat-based Linux distribution like CentOS or Fedora, the installation process is equally simple but uses a different package manager called yum. Install curl by executing:
Verify Installation
In both cases, after installation is complete, you can check the Curl version to confirm it was installed correctly. The code is:
Basic Curl Commands
Alright, let’s now start with some of the basic commands that will get you up and running.
1: Retrieve a Web Page
Curl’s most basic application is to fetch a web page. Instead of entering a website address into your browser, you can use Curl to directly retrieve the same page from your terminal. Here’s how you do it:
This will display the HTML content of the page in your terminal. If you just want to check if the page is accessible and don’t want to see all the HTML code, you can use the -I option to fetch just the headers.
2: Download Files
You can also use this command to download a file directly from the terminal. Use the -O option and specify the URL of the file.
3: Save Output to a File
When you use Curl to fetch a web page, it displays the content in the terminal. If you want to save this content to a file, use the -o option.
This command will save the HTML content of the web page to a file called output.html.
4: Limit the Transfer Rate
There may be situations where you don’t want Curl to use all your bandwidth. Maybe you are doing something else that’s more important. Use –limit-rate to limit the transfer rate.
This limits the download rate to 100 KB per second.
5: Use a Proxy
If, for some reason, you need to use a proxy, Curl can handle that too. Use the -x option followed by the proxy details.
6: Measure Time Taken
Wondering how long a request takes? Curl can give you this information with the -w option.
Conclusion
Curl is a powerful tool for working with web data. With its abundance of options, the curl command offers versatility and can be used in numerous distinct ways. It’s especially valuable for developers and system administrators for tasks like downloading files, interacting with APIs, and troubleshooting network issues.