Wget is a free Linux command-line utility tool that helps us retrieve or download files from the internet. It retrieves files using FTP, HTTP, and HTTPS protocols. The command can sustain unstable and slow network connections. If a network interruption occurs during a download, wget can resume the download from where it halted. Wget is non-interactive therefore it can work in the background. This allows you to start a download, disconnect from the system and let wget complete it. In this guide. you will learn how to use the Linux wget command.
The syntax:
Wget command follows the syntax below.
$ wget [option][URL]
The [option] flag lets you specify the action to perform with the wget command. The [URL] flag points to the address of the directory, file, or webpage that you wish to download.
1) Check if wget is installed
Wget comes preinstalled in most Linux systems. To check if installed type the following command in your terminal:
$ wget
If wget is installed, the output notifies you that the wget command is missing a URL, as shown below.
If you get the output “wget command not found’, download and install it manually.
The installation instructions for different Linux distros:
For Ubuntu/Debian
To install wget on Ubuntu or Debian-based distributions. Run the command:
$ sudo apt-get install wget
For CentOS/RHEL
To install wget on CentOS / RHEL, run the command
$ sudo yum install wget
For Fedora 22 and later versions
To install wget on Fedora, run the command:
$ sudo dnf install wget
Now let’s shift gears and have a look at a few examples of the Linux wget command.
1) Download files using the wget command
To download a file from the web, use the syntax:
$ wget [URL]
For example, to download the latest WordPress zip file, run the command:
$ wget https://wordpress.org/latest.zip
2) Download File to a Specific Directory
By default, the wget command downloads a file in the directory that the user is currently in. To save the file in a different location, add the –P option:
$ wget –P [destination_directory] [URL]
For example, to install Git on Ubuntu, you can select to download the zip package in the /tmp directory as shown.
$ wget –P /tmp https://github.com/git/git/archive/master.zip
3) Download Multiple Files
Additionally, you can download multiple files concurrently using the -i option. The URLs are saved in a text file and the syntax below is invoked where the file_name is the file containing the URLs.
$ wget –i [file_name]
In the following example we are downloading Ubuntu, Fedora, and CentOS ISO images:
So, first, create the file linux.txt.
$ vim linux.txt
Then append the URLs of the packages you want to download in the editor each in its own line. Once done, save and exit the text editor.
And finally run the following command to download the multiple files in the nano file we just created:
$ wget -i linux.txt
4) Download File and Save Under Specific Name
To download a file and save it under a specified name in your system, use the -o option in the syntax shown
$ wget –O [file_name] [URL]
For example to download tomcat and save it under tomcat.zip:
$ wget -O tomcat.zip http://apache.cs.utah.edu/tomcat/tomcat-9/v9.0.20/bin/apache-tomcat-9.0.20.tar.gz
5) Resuming a download
wget can resume a download where it stopped before the interruption using the -c option as shown
$ wget –c [URL]
In the following example, we are resuming the download of the Ubuntu ISO file after an interrupted connection:
$ wget –c https://releases.ubuntu.com/20.04/ubuntu-20.04-desktop-amd64.iso
6) Set Download Speed using wget
To set the download speed, so that it does not use the full available bandwidth use the syntax:
$ wget --limit-rate [wanted_speed] [URL]
The download speed can be set in kilobytes (k) and megabytes(m), gigabytes(g).
For example, To download terraform and limit the speed rate to 500 kilobytes:
$ wget --limit-rate 500k https://releases.hashicorp.com/terraform/0.12.2/terraform_0.12.2_linux_amd64.zip
7) Creating a mirror of a website
To download an entire website from the internet in HTML with wget, use the –m option. This creates a local copy of the website as well as the website resources (JavaScript, CSS, Images). Use the syntax:
$ wget –m [URL]
For example:
$ wget –m https://sample.com
In most cases, you can pass a few arguments to the command above. The -k option can make the website display in a better format. The -p option tells wget to download the necessary files for displaying the HTML page.
$ wget -m -k -p https://sample.com
8) Download in Background
Wget is non-interactive and can download a file in the background. Use the -b option as shown:
$ wget –b [URL]
In the following example, we are downloading the RPM package manager in the background:
$ wget –b http://some_website/sample_file.rpm
Conclusion
This wraps up our guide today on the Linux wget command.