How to download a file using curl in Linux?

LinuxLinuxBeginner
Practice Now

Introduction

In this tutorial, we will explore the powerful capabilities of the curl command in the Linux operating system. Curl is a versatile tool that allows you to transfer data using various protocols, including HTTP, FTP, and more. We will focus on how to use curl to download files from the web, providing you with the knowledge and techniques to streamline your file transfer processes on Linux.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux/PackagesandSoftwaresGroup -.-> linux/curl("`URL Data Transferring`") linux/PackagesandSoftwaresGroup -.-> linux/wget("`Non-interactive Downloading`") subgraph Lab Skills linux/curl -.-> lab-409838{{"`How to download a file using curl in Linux?`"}} linux/wget -.-> lab-409838{{"`How to download a file using curl in Linux?`"}} end

What is Curl?

Curl, short for "Client URL", is a powerful command-line tool used for transferring data over various protocols, including HTTP, FTP, SFTP, and more. It is a versatile tool that can be used for a wide range of tasks, such as downloading files, uploading data, testing APIs, and more.

Curl is available on most Linux distributions and can be installed using the system's package manager. For example, on Ubuntu 22.04, you can install Curl using the following command:

sudo apt-get install curl

Once installed, you can use Curl to perform various operations, including downloading files from the internet. Curl provides a simple and efficient way to download files without the need for a graphical user interface (GUI) or a web browser.

Why Use Curl for File Downloads?

Curl is a popular choice for downloading files in Linux for several reasons:

  1. Command-Line Interface: Curl provides a command-line interface, making it easy to integrate into scripts and automate file downloads.
  2. Versatility: Curl supports a wide range of protocols, allowing you to download files from various sources, including HTTP, FTP, SFTP, and more.
  3. Customization: Curl offers a wide range of options and parameters that allow you to customize the download process, such as setting headers, handling redirects, and more.
  4. Lightweight: Curl is a lightweight tool that doesn't require a graphical user interface, making it suitable for use on servers and other headless systems.

Curl Download Syntax

The basic syntax for downloading a file using Curl is as follows:

curl [options] <url>

Here, [options] represents any additional parameters or flags you want to use, and <url> is the URL of the file you want to download.

For example, to download a file from the URL https://example.com/file.zip, you can use the following command:

curl https://example.com/file.zip

This will download the file and save it to the current working directory with the same filename as the remote file.

Downloading Files with Curl

Basic File Download

To download a file using Curl, you can simply provide the URL of the file as an argument:

curl https://example.com/file.zip

This will download the file and save it to the current working directory with the same filename as the remote file.

Specifying the Output Filename

If you want to save the downloaded file with a different filename, you can use the -o or --output option:

curl -o local_file.zip https://example.com/file.zip

This will download the file from the provided URL and save it as local_file.zip in the current working directory.

Resuming Interrupted Downloads

Curl also supports resuming interrupted downloads. If the download is interrupted for any reason, you can resume the download from the point where it left off by using the -C - option:

curl -C - -o local_file.zip https://example.com/file.zip

This will resume the download of the file from the point where it was interrupted.

Saving the Output to a File

Instead of printing the downloaded content to the console, you can save the output to a file using the > operator:

curl https://example.com/file.zip > local_file.zip

This will download the file and save it to local_file.zip in the current working directory.

Handling Redirects

Curl can automatically follow redirects during the download process. If the URL you provide redirects to another URL, Curl will follow the redirect and download the file from the final URL. You can use the -L or --location option to enable this behavior:

curl -L https://example.com/redirect

This will follow any redirects and download the file from the final URL.

Advanced Curl Options for File Downloads

Setting Request Headers

Curl allows you to set custom HTTP headers for your file download requests. This can be useful when the server requires specific headers to be present. You can use the -H or --header option to set headers:

curl -H "User-Agent: LabEx Bot" https://example.com/file.zip

This will include the User-Agent header with the value LabEx Bot in the request.

Handling Authentication

If the file you're trying to download is protected by authentication, you can use Curl to provide the necessary credentials. For example, to download a file that requires basic authentication:

curl -u username:password https://example.com/protected_file.zip

This will include the basic authentication credentials in the request.

Limiting Download Speed

Curl allows you to limit the download speed to a specific value, which can be useful for controlling bandwidth usage. You can use the --limit-rate option to set the maximum download speed:

curl --limit-rate 500k https://example.com/large_file.zip

This will limit the download speed to 500 kilobytes per second.

Monitoring Download Progress

Curl can display a progress meter during the download process, which can be helpful for monitoring the download status. You can enable the progress meter by using the -# or --progress-bar option:

curl --progress-bar https://example.com/file.zip

This will display a progress bar during the download.

Handling Errors and Retries

Curl provides options to handle errors and automatically retry failed downloads. For example, you can use the --retry option to specify the number of times Curl should retry a failed download:

curl --retry 3 https://example.com/flaky_file.zip

This will retry the download up to 3 times if the initial download fails.

By combining these advanced options, you can create more robust and customized file download workflows using Curl in your Linux environment.

Summary

By the end of this tutorial, you will have a solid understanding of how to use curl to download files in your Linux environment. You will learn the basic syntax, explore advanced options, and gain the skills to efficiently transfer files from the web to your local system. Whether you're a developer, system administrator, or a casual Linux user, this guide will equip you with the necessary knowledge to leverage the power of curl for your file download needs.

Other Linux Tutorials you may like