In this tutorial, let’s learn how to download a file using the wget command in Linux. For any Linux user, downloading files is a frequent task. While you can download things using your browser, it is helpful to know the steps to download a file in Linux using the command line. this is where the wget command comes into the picture.
Basics of the wget Command in Linux
The wget utility comes pre-installed in most Linux distributions. However, if it is not the case for your system, you can download it using the package manager for your distribution.
To check this, we will ask the system to display the version of the wget command. This can be done by typing this in your terminal.
If this does not show you the version of wget command on your system, you need to install it. Before we begin to install wget command in Linux, we need to make sure that all our system repositories are up-to-date.
To do this, we need to update our default repositories using the apt package management service. Open the command line on your system and type the following.
sudo apt update
Now, you can install the wget command on your system depending on the distribution you are using.
For Debian and Ubuntu-based systems, use the following command.
sudo apt install wget
For CentOS and Fedora-based systems, use the following command.
sudo yum install wget
Understanding the wget Command
The wget command in Linux is a command used to download files from the web. This command uses the URL of a file to download it. The command can be used to download data over HTTP, FTP, and HTTPS protocols.
We can use this to directly download files from our command line without needing a web browser. Further, wget is a non-interactive utility.
This enables a user to download a file in the background. It can work even when a user is has logged off. This is highly important in an instance when you are downloading a large file.
In case the download fails, the wget utility will repeatedly try to resume it till the complete file has been downloaded. If the server allows, it will continue the download from the last point where it left off.
Using the wget Command in Linux
The best way to understand any command is through understanding it’s syntax. Here is how the syntax for the wget command in Linux looks like.
wget [option] [URL]
Here, we use the URL of the file which we wish to download. The wget utility allows several options to set up our download. Here is a list with some of the commonly used options offered by the wget command in Linux.
|-h||This option is used to display the help message for the wget command. It includes all the possible options which can be used with the wget command in Linux|
|-b||This option is used to send the download process to the background as soon as it is initiated with the wget command. It frees up your terminal session to run other commands.|
|-i||This option is used to read the URL for the wget command from a file. It eliminates the need for the wget command to have an URL and allows the inclusion of an input file.|
|-o||Name of the output file if you do not want the same name as the server has provided|
|-c||This tag makes wget keep a track of the download progress in case of download interruptions. We can continue an interrupted download if this option was used when the download was initiated.|
|-tries=n||This option is used to limit the number of tries for the wget command. Using this command will cause the wget utility to retry a download only ‘n’ times. The default is 20.|
|-limit-rate=maxlim||This option is used to limit the download speed for the wget command. This helps the user to dictate the amount of bandwidth to be allocated for a download.|
While this list covered the basic options used in the wget utility. You should explore the help page by using the -h option to understand the full potential of the wget command.
How to Download Files Using the wget Command?
Now we have developed an understanding of the wget command in Linux and its parameters. Now it’s time to use this knowledge to download files using the wget command. For this tutorial, we will go over a couple of examples.
Downloading a file over HTTP
The simplest use of the wget utility is to download a single file. For this example, we will download an iso file.
wget -c <file URL>
In the above example, I’m downloading the Kali Linux ISO file for demonstration along with the -c tag. Since this is a pretty large file, if the ISO file download is interrupted for any reason, I can run the same command in the same directory and the download will continue from where it left off.
Downloading Files from an FTP server
You don’t need to connect to an FTP server using the ftp command to download files. This can be done while using the wget utility. The difference between downloading a file from HTTP vs an FTP server is the possibility that there’s a username and password requirement.
Let’s use the wget command to login to the FTP server and download a file.
wget --user='FTP username' --password='FTP server password' <FTP URL>
As you can see in the above image, I have connected to one of the free FTP servers on the internet with their demo username and password. The highlighted section shows that the login was successful.
The wget command in Linux is a useful tool to download files from the web without having to leave the command line. Its appealing features and robust functionality is the reason behind its popularity amongst Linux users, from beginners to veterans.
We hope this tutorial was able to help you understand the wget command in Linux. We discussed only the basic usage of the wget command in this tutorial, so make sure to explore the command on your own.
If you have any feedback, queries, or suggestions, feel free to reach out to us in the comments below.