Download a file in parts with curl in Ubuntu

There could be multiple reasons why one would want to download a larger file in parts. One of the most common reason affecting users is limited bandwidth available. In this article, we would discuss how to download a file in parts with curl in Ubuntu. curl – a command-line utility is used to transfer data from/to a server. Although curl offers numerous features but for the purpose of this article we would only discuss its -range option.

Install curl in Ubuntu

The following operations would require you to have superuser privileges. In case, you don’t have one then contact your System Administrator for assistance. curl package is already available in standard Ubuntu repository. We need to update the repository to get the latest version available first thereafter, we will install the package. Run the following in terminal –

sudo apt update
sudo apt install curl

Download a file in parts with curl

We would discuss the steps involved with the help of an example. Lets say we are about to download xyz-desktop-amd64.iso. The download size is of about 2.0GB. For some reason, we can download the file only in parts of 500MBs. Therefore, we would use curl command-line utility with -range option defining the size of download for each of the parts.

First, we will discuss the syntax for the command –

curl --range start-stop -o file-name.partn <url>

where,

curl is the command-line utility,
– – range specifies the range of the file to be downloaded,
start-stop to define the start and stop byte range,
-o is for output,
file-name-n.part is the output for part file downloaded, and
<url> from where the file gets downloaded.

So, it may look like –

curl --range 0-499999999 -o xyz.part1 http://<domain-name>/xyz-desktop-amd64.iso

This downloads the file size of approximately 475 MBs. We have not chosen the exact file size of 500MBs to make it less tedious.

Similarly for subsequent parts –

curl --range 500000000-999999999 -o xyz.part2 http://<domain-name>/xyz-desktop-amd64.iso
curl --range 1000000000-1499999999 -o xyz.part3 http://<domain-name>/xyz-desktop-amd64.iso
curl --range 1500000000- -o xyz.part4 http://<domain-name>/xyz-desktop-amd64.iso

In the last instruction we have not specified any stop range. It will download the remaining part of the file itself.

Lastly, we need to combine different parts of the downloaded file. To get the desired outcome, we would use cat command.

Join downloaded parts of our file

First, we will discuss the syntax of the command.

cat download-file.part > file-name.original

where,

cat is the command-line utility used to join files,
download-file.part are parts which we have just downloaded, and
file-name.original the original name of our file.

We will continue with above example to make things more clear.

cat xyz.part* > xyz-desktop-amd64.iso

In conclusion, we have discussed how to download parts of a file with curl and subsequently join them to get the desired output.

Similar Posts