In this guide, we intend to teach you Examples To Use the Wget Command in a Linux Terminal. Let’s see what the wget command is and the usage of it. Then, start to use this amazing command line utility.
What is Wget Command Used for in Linux?
The wget command is a command line utility for downloading files from the Internet. It supports downloading multiple files, downloading in the background, resuming downloads, and limiting the bandwidth used for downloads and viewing headers.
Also, it can be used for taking a mirror of a site and can be combined with other UNIX tools to find out things like broken links on a site.
Examples To Use Wget Command in Linux Terminal
This guide will show you how to use the wget command with the most useful commands and options examples.
The wget package is pre-installed on most Linux distributions today.
To check whether the Wget package is installed on your system, open up your console, type wget, and press enter. If you have wget installed, the system will print wget: missing URL. Otherwise, it will print the wget command not found.
If you don’t have the wget command on your Linux distro, you can easily install it on your server with the Linux Commands.
Install Wget on Debian / Ubuntu
You can simply, run the command below to install wget in Debian-based distros:
sudo apt install wget
Install Wget on Centos / Fedora / AlmaLinux / RHEL
On RHEL-based distros like Centos, AlmaLinux, Rocky Linux, and Fedora, you can use the command below to install wget:
# sudo yum install wget
# sudo dnf install wget
General Syntax of Wget Command
At this point, we want to show you the basic syntax of the wget command. It looks like this:
wget [OPTION]… [URL]…
Then, proceed to the next step to see how to use it on your Linux system.
Download a File with Wget Command in Linux
When you use the wget command without any options, it will download the resource specified in the [url] to the current directory.
Rename a Downloaded File with Wget
You can save your file under a different name by using the “-O” option. For example:
wget -O latest.php http://example.com/sample.php
This command will save the file with the name of the latest.php instead of the original name.
Save a Downloaded File with Wget in a Different Directory
By default, wget will save the downloaded file in the current working directory. To save the file to a specific location, use the “-P” option in the wget command in Linux. For example:
wget -P /opt/download http://example.com/sample.php
This command will save the file in the/opt/download directory instead of the working directory.
Limit Download Speed with the Wget Command
By default, the speed is measured in bytes/second. You can easily limit your download speed by using the “–limit-rate” option. For example:
wget --limit-rate=1m http://example.com/sample.php
This will limit the download speed to 1MB.
You can use k for kilobytes, m for megabytes, and g for gigabytes.
Note: This option is useful when you don’t want wget to consume all the available bandwidth.
Resume a Download with Wget Command
If your connection drops while downloading a file, you can resume your downloading by using the “-c” option. For example:
wget -c http://example.com/sample.php
Note: If the remote server does not support resuming downloads, wget will start the download from the beginning and overwrite the existing file.
Run the Wget Command in the Background
If you plan to download a file in the background, you can use the wget command in Linux by using the “-b” option. For example:
wget -b http://example.com/sample.php
By default, the output is redirected to the wget-log file in the current directory. To watch the status of the download, you can use the tail command:
tail -f wget-log
Download Multiple Files with the Wget Command
Here you can use the “-i” option in the wget command to download multiple files at once.
The “-i” option is used to read URLs from the file. If -i is specified as a file, URLs are read from the standard input. If this function is used, no URLs need to be present on the command line. Also, if there are URLs both on the command line and in an input file, those on the command lines will be the first ones to be retrieved. The file need not be an HTML document if the URLs are just listed sequentially.
# wget -i inputfile
# wget -i inputfile [URL]
Download a File to a Standard Output
In the following example, wget will quietly ( flag -q) download and output the latest WordPress version to stdout ( flag -O –) and pipe it to the tar utility, which will extract the archive to the /var/www directory.
wget -q -O - "http://wordpress.org/latest.tar.gz" | tar -xzf - -C /var/www
Wget Command Options
For more help, you can use the following command:
In your output, you will see:
As you can see, there are many options that you can use with the Wget command line utility.
At this point, you have learned Examples To Use the Wget Command in the Linux Terminal and the most widely used options. This command line utility has many options that you can use to do your tasks. Hope you enjoy it.
Also, you may interested in these articles: