Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

command line
windows
linux

How to download files using the command-line interface (CLI)

Faraz Karim

Why the command line is more efficient

When you need to download a file on the internet, like an image, you would need to open up your browser, go to the specified URL, locate the required file, then download it. This not only takes up more time than it should; it also puts an undue burden on your system resources.

An alternative that remedies these issues is to download the files using the system command-line interface, the CLI. Performing a download using the CLI is much faster and only requires you to enter a single command, though you do need to know the exact URL where the file is stored. This can be done on any operating system, but we will be concentrating on Windows and Linux in this shot.

Windows

In Windows, to download a file from the internet through the CLI, you must first install a tool that enables this functionality. There are several tools and commands for this purpose, including bitsadmin, wget, Invoke-WebRequest, and curl. We will be using curl in our example, due to its ease of use.

Newer versions of Windows are shipping with curl pre-integrated, so if you have a Windows Version 1803 or above, you can skip the setup section and head straight to the Download commands section.

Setup

Before we can start using curl, we need to install it on our system.

This is slightly tricky since you need to download and put specific files in specific directories in your system. Messing up any step would result in curl not working properly. The easiest way is to download a package manager and let it handle everything. Nearly any package manager will do, but I would recommend chocolatey for its wide support and relative ease of installation.

With the chocolatey package manager installed, open your CLI by typing cmd in the windows search box or pressing Windows + R. Once the command dialogue box opens up, write the following command in it:

choco install curl --version=7.77.0

You need to use the latest version in this command. To find which version this is, visit the official curl website, navigate to the Windows section towards the bottom, and check the version mentioned.

Download command

Now, with curl installed, running the following command will download your file from the internet:

Here, you need to replace the <file_URL> with the actual URL of the file you want to download. For example, if you want to download a jpeg image named coolpic from the site educative.io, the following would be the command you run:

curl http://educative.io/coolpic.jpg

You could also change the name and set the directory of this downloaded picture using the O flag:

curl http://educative.io/coolpic.jpg -O home/myfolder/newcoolpic.jpg

Linux

For Linux users, things are quite a bit easier since Linux ships with a package manager pre-installed. In most distributions, curl also comes preinstalled, but you have to enter the command curl in the terminal to check if it is installed.

Setup

In case it is not installed, running the following command will install it:

$ sudo apt install curl

Download command

Similarly to how we used the curl command in windows, Linux also uses the same format:

Here, again, you need to replace the <file_URL> with the actual URL of the file you want to download. For example, if you want to download a jpeg image named coolpic from the site educative.io, the following would be the command you run:

curl http://educative.io/coolpic.jpg

You could also change the name and set the directory of this downloaded image using the O flag:

curl http://educative.io/coolpic.jpg -O home/myfolder/newcoolpic.jpg

Here, it is a little different from Windows in the order of the arguments. The O flag and its argument come before the URL in Linux.

RELATED TAGS

command line
windows
linux

CONTRIBUTOR

Faraz Karim
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring