We’ll show you 10 practical examples of the Wget Command. Wget is a free utility that can be used for retrieving files using HTTP, HTTPS, and FTP which are considered as the most widely-used Internet protocols.
Its name comes from World Wide Web + get. Wget has many features which makes it a very easy task when it comes to retrieving large files, recursive downloads, multiple file downloads, or mirroring entire web or FTP sites.
Wget is non-interactive which gives great flexibility in using it. It can be easily called from scripts, cron jobs, terminals, etc. It can work in the background even if a user is not logged in. This allows you to start a file download and disconnect from the system, letting wget finish the work.
In this article, we will demonstrate the use of wget through some practical examples that you can use to accomplish some of the most common tasks such as downloading files or even mirroring entire websites.
For the purpose of this demonstration, we will install wget on an Ubuntu 16.04 VPS. Please note that even though this has been tested on Ubuntu 16.04, the instructions can be used on any other Linux distribution as well.
The first step is to log in to your server via SSH.
You can also make sure that your server is up to date with the following commands:
apt-get update apt-get upgrade
After the upgrades have been installed, you can then install the wget software package with the following command:
apt-get install wget
After the installation is completed, you can now start using the wget command on your server.
Table of Contents
Wget command example #1 – Download a single file
The most common and simple usage of wget is to download a single file and store it in your current directory.
For example, to download the latest WordPress version you can use the following command:
This is the output you will get while the file is downloading:
--2017-10-14 03:46:06-- https://wordpress.org/latest.zip Resolving wordpress.org (wordpress.org)... 126.96.36.199, 188.8.131.52 Connecting to wordpress.org (wordpress.org)|184.108.40.206|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 8912693 (8.5M) [application/zip] Saving to: 'latest.zip' latest.zip 100%[=====================================================================================================>] 8.50M 5.03MB/s in 1.7s 2017-10-14 03:46:07 (5.03 MB/s) - 'latest.zip' saved [8912693/8912693]
As you can see, it also shows you the download progress, current download speed, size, date, time, and the name of the file.
In our case, this command will download the file and save it in your current directory under the “latest.zip” name.
Wget command example #2 – Download a file and save it under a different name
You may want to save the file under a different name. To do this, you can use the -O option like this:
wget -O wordpress.zip https://wordpress.org/latest.zip
This will download and save the latest WordPress installation in your current directory under the name “wordpress.zip”.
Wget command example #3 – Download a file and save it in a specific directory
To download the file and save it in a different directory, you can use the -P option, for example:
wget -P /opt/wordpress https://wordpress.org/latest.zip
This will download and store the file in the /opt/wordpress directory on your server.
Wget command example #4 – Set the download speed
If you happen to download a huge file that takes longer to complete, you can also limit the download speed to prevent wget from using the full possible bandwidth of your connection.
To limit the download speed to 300k, for example, you can use the following command:
wget --limit-rate=300k https://wordpress.org/latest.zip
Wget command example #5 – Continue interrupted download
Sometimes, when you download a very big file that would take a longer time to complete, you may temporarily lose your internet connection and your download will get interrupted.
To avoid starting the whole download again, you can continue from where it got interrupted using the -c option:
wget -c https://wordpress.org/latest.zip
If the download is interrupted, and you start the whole download again without the -c option, wget will append “.1” to the end of the filename because the filename with the previous name already exists.
Wget command example #6 – Download in background
For bigger files, you can also use the -b option, to download the file in the background.
wget -b http://example.com/big-file.zip
The output will be written in the “wget-log” file in the same directory, and you can always check the status of the download with the following command:
tail -f wget-log
Wget command example #7 – Increase retry attempts
If you are having issues with your internet connection, and your download is getting interrupted multiple times, you can increase the retry attempts to download the file with the -tries option:
wget -tries=100 https://example.com/file.zip
Wget command example #8 – Download multiple files
If you want to download multiple files at the same time, you can create a text file (for example download.txt) where you place all the URLs of the files you wish to download. To create a text file do the following:
Then you can edit the file with nano, and enter all the URLs of all the files you want to download:
http://example.com/file1.zip http://example.com/file2.zip http://example.com/file3.zip
After you save the file, you can then use the -i option to download all the files stored in that text file:
wget -i download.txt
Wget command example #9 – Download through FTP
You can also use wget to download a file directly through FTP using a set username and password, with the following command:
wget --ftp-user=username --ftp-password=password ftp://url-to-ftp-file
Wget command example #10 – Download the entire website
You can even use wget to download an entire website, which you can then view locally, offline, without the need of an internet connection. To do this you can use the following command:
wget --mirror --convert-links --page-requisites ----no-parent -P /path/to/download https://example-domain.com
With —mirror, you turn on all the options required for mirroring.
With –convert-links, you will convert all the links so they would work offline.
With –page-requisites, you download all the necessary files such as CSS style sheets and images required to properly display the pages offline.
With –no-parent, you can restrict the download only to a specific portion of the site.
Additionally, you can set the path to where we want to download the files with the -P command followed by the path to the directory.
We have covered some of the most common uses of wget command in 10 practical examples. To learn more about wget, you can always check its man page with the man wget command.
If you have a Linux VPS with us, all you have to do is ask our expert Linux admins to install wget on your server or to offer some advice Wget Command Examples. They’re available 24/7 and will be able to help you solve this issue.
PS. If you enjoy reading our post, on Wget Command Examples, feel free to share it on social networks using the shortcuts below, or simply leave a comment.