How to download a file using cURL on Linux?

While wget is the most commonly used utility to download files, cURL and aria2 can be used to achieve the same results as well.

cURL works both as a command line utility and a library and therefore you can use it to download and transfer any kind of data over different protocols such as HTTPS, SFTP, HTTP, FTP and many more. In this tutorial, I will show you the different use cases of this command line tool with examples, so fasten your seatbelts and let’s get started!

Also read – The Difference between Wget and cURL command

Using cURL to download a file.

Simply type the following command if you wish to download a file without any extra parameters :

curl https://your-domain/application.exe

If you want to specify any protocol, then you can type :

curl sftp://sftp-website-name/file.txt

Let’s say you want to follow a 301-redirected file while downloading, you can use the -L flag :

curl -L http://a-website/sourcecode.tar.gz
Using Curl To Download A File
Using Curl To Download A File

You can also download a file and immediately rename it using -o flag like this :

curl -L -o file.txt http://sample-website/very.long.file.name.txt

For example, let’s download an application named Freetube from GitHub :

Downloading A File And Renaming It
Downloading a file and renaming it immediately

You can also apply a speed limit to your downloads using the –limit-rate flag. For example :

curl -L -O --limit-rate 1m https://github.com/FreeTubeApp/FreeTube/releases/download/v0.17.1-beta/freetube-0.17.1-linux-portable-arm64.zip
The Download Took Some Time Because Of The Speed Limit
The Download Took Some Time Because Of The Speed Limit

In the above example, I have set the download rate to not exceed 1MB per second.

Summary

cURL is a really lightweight tool full of features that any other terminal-based downloader might not have. However, to read more about its functions, you should definitely check out the manual page of this command, just type :

man curl
Manual Page Of The Curl Command
Manual Page Of The Curl Command

How can I download files using cURL?

You can download files with cURL by using the command `curl -O [URL]`, which saves the file with its original name. For example, `curl -O http://example.com/file.txt` will download the file and save it to your current directory.

How do I output to a file with cURL?

To output to a file with cURL, you can use the `-o` option followed by the desired filename. For example, `curl -o myfile.txt http://example.com/file.txt` will download the content and save it as `myfile.txt`.

Can I download multiple files with cURL?

Yes, cURL allows you to download multiple files by listing the URLs in a single command. You can use `curl -O [URL1] -O [URL2]` or create a text file with all URLs and use `curl -O -K myurls.txt` to download them sequentially.

How do I see the progress bar while downloading a file with cURL?

By default, cURL shows a progress bar while downloading files. You can also use the `–progress-bar` option for a more succinct display of the download progress.

What should I do if I want to interrupt a cURL request?

You can interrupt an ongoing cURL request by pressing `Ctrl+C` in the terminal. If you need to resume the download later, use the `-C -` option along with the original cURL command.

How can I debug cURL requests?

To debug cURL requests, you can use the `-v` (verbose) option, which provides detailed information about the request and response headers. This is helpful for troubleshooting issues with the server or connection.

Is it possible to download files via FTP using cURL?

Yes, cURL supports FTP and you can download files using the command `curl -u username:password ftp://example.com/file.txt -O`. Make sure to replace `username`, `password`, and `URL` accordingly.

Can cURL handle cookies when downloading files?

Yes, cURL can handle cookies. You can use the `-b` option to send cookies to the server and the `-c` option to save cookies from the server to a file for future requests.

How do I download a binary file with cURL?

To download a binary file with cURL, simply use the `-O` option as you would for any other file. cURL automatically determines the content type based on the server response, ensuring proper handling of binary data.

Can I use cURL in a Python script for downloading files?

Yes, you can use cURL within a Python script by calling it through the subprocess module. Alternatively, you can use libraries like `requests` in Python, which offer a more straightforward way to handle HTTP requests and downloads.