How to Use wget Command in Linux
-
Check Whether
wget
Is Installed or Not -
Syntax of
wget
Command -
Download File With
wget
-
Save the File Downloaded Using
wget
With a Custom Filename -
Save the Downloaded File Using
wget
to a Specific Directory -
Specify Downloading Speed Using
wget
-
Resume the Download Using
wget
-
Download Files in Background Using
wget
-
Change the
wget
User-Agent of thewget
-
Download Multiple Files Simultaneously Using
wget
-
Download Files Using the
wget
Command via FTP -
Create a Copy of the Website Using
wget
- Skip Certificate Check While Using Wget
![How to Use wget Command in Linux](/img/Linux/ag-feature-image---How-to-use-wget-command-in-linux.webp)
We can use the wget
command in Linux to download files from the Internet using the HTTP, HTTPS, and FTP protocols with various options such as downloading multiple files at once, downloading in the background, limiting the bandwidth, and much more.
Check Whether wget
Is Installed or Not
We can check whether the wget
is installed or not in our system by simply entering the wget
command in our terminal.
If wget
is installed in our system, we get an output as:
wget: missing URL
Usage: wget [OPTION]... [URL]...
Try 'wget --help' for more options.
If wget
is not installed in our system, we get an output as:
Command 'wget' not found, but can be installed with:
sudo snap install wget
If wget
is not installed in our system, we can install it using the following commands:
Install wget
for Ubuntu and Debian
sudo apt install wget
Install wget
for CentOS and Fedora
sudo yum install wget
Syntax of wget
Command
wget [options] [url]
options
are various options available to customize the download process and url
refers to the URL
from where the file needs to be downloaded or synchronized.
Download File With wget
To download a file using the wget
command, we simply specify the URL of the file repo after wget
.
wget https://bloximages.newyork1.vip.townnews.com/redandblack.com/content/tncms/assets/v3/editorial/4/59/45940eb2-5403-11e9-a843-db0e4491cc90/5ca13d8453042.image.jpg
Output:
--2020-09-28 19:29:15-- https://bloximages.newyork1.vip.townnews.com/redandblack.com/content/tncms/assets/v3/editorial/4/59/45940eb2-5403-11e9-a843-db0e4491cc90/5ca13d8453042.image.jpg
Resolving bloximages.newyork1.vip.townnews.com (bloximages.newyork1.vip.townnews.com)... 104.18.130.43, 104.18.131.43
Connecting to bloximages.newyork1.vip.townnews.com (bloximages.newyork1.vip.townnews.com)|104.18.130.43|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 33241 (32K) [image/jpeg]
Saving to: '5ca13d8453042.image.jpg'
5ca13d8453042.image 100%[===================>] 32.46K --.-KB/s in 0.003s
2020-09-28 19:29:15 (11.1 MB/s) - '5ca13d8453042.image.jpg' saved [33241/33241]
It downloads the image file at the given URL and then saves the downloaded file in our current working directory.
The command at first resolves the IP address of the URL and then starts the download. We also can see the download progress bar in the terminal.
If we don’t want to see any output in the terminal, we can use the -q
option with the wget
command.
Save the File Downloaded Using wget
With a Custom Filename
By default, the name of the downloaded file is the same as it was on the web. If we want to save the file with a different filename, we can specify the filename after the -O
flag.
wget -O linux.jpg https://bloximages.newyork1.vip.townnews.com/redandblack.com/content/tncms/assets/v3/editorial/4/59/45940eb2-5403-11e9-a843-db0e4491cc90/5ca13d8453042.image.jpg
It saves the downloaded file in our current working directory with the filename linux.jpg
.
Save the Downloaded File Using wget
to a Specific Directory
By default, all the downloaded files get saved in the current working directory. To save the downloaded files in a specific directory, we use the -P
option followed by the directory location where the downloaded file needs to be saved.
wget -P Downloads/Linux_Images https://bloximages.newyork1.vip.townnews.com/redandblack.com/content/tncms/assets/v3/editorial/4/59/45940eb2-5403-11e9-a843-db0e4491cc90/5ca13d8453042.image.jpg
It saves the downloaded image in the Linux_Images
directory inside the Downloads
directory.
Specify Downloading Speed Using wget
If we don’t want to use all the available bandwidth for downloading files using wget
, we can use the --limit-rate
option to restrict the downloading rate of the files.
wget -P --limit-rate=5m https://bloximages.newyork1.vip.townnews.com/redandblack.com/content/tncms/assets/v3/editorial/4/59/45940eb2-5403-11e9-a843-db0e4491cc90/5ca13d8453042.image.jpg
It limits the download rate to 5mb.
Resume the Download Using wget
If our internet connection suddenly goes away amidst the download of a large, download the file from the beginning again would be a tedious task. In such cases, we can resume the download using the -c
option.
wget -c https://bloximages.newyork1.vip.townnews.com/redandblack.com/content/tncms/assets/v3/editorial/4/59/45940eb2-5403-11e9-a843-db0e4491cc90/5ca13d8453042.image.jpg
In some cases, the server might not support resuming downloads, and in such case, the downloading will begin from the start.
Download Files in Background Using wget
To download files in the background using wget
, we add -b
option to the command.
wget -b https://releases.ubuntu.com/20.04/ubuntu-20.04.1-desktop-amd64.iso
It will download Ubuntu 20.04 image file in the background.
To watch the download status of the file, we use the following command:
tail -f wget-log
Change the wget
User-Agent of the wget
In some cases, the server may block the wget user agent. In such cases, we can emulate a different browser using the -U
option.
get --user-agent="Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0" <url>
In this case, Mozilla 68 will request the file from the <url>
.
Download Multiple Files Simultaneously Using wget
To download multiple files using wget, we make a .txt
which lists URLs for each file to be downloaded on a separate line. We then use the wget
command with the -i
option followed by the file’s path containing URLs.
wget -i files.txt
This will download all the files from the URLs specified in the files.txt
file.
Download Files Using the wget
Command via FTP
To download files using the wget command via FTP, we specify the username using the --ftp-user
option and password using the --ftp-password
option.
wget --ftp-user=username --ftp-password=password <url>
It downloads the file from the specified URL using the FTP
protocol.
Create a Copy of the Website Using wget
To create a copy of a website using wget
, we use -m
option followed by the URL whose copy is to be made.
wget -m https://abc.com
This will create a copy of abc.com
by downloading all the inner links and static files required to render the website.
If we wish to run the downloaded website, we also need to add -k
and -p
options.
wget -m -k -p https://abc.com
Skip Certificate Check While Using Wget
Sometimes we need to download a file from the host without a valid SSL certificate using HTTPS protocol. In such cases, we use the --no-check-certificate
option to skip certificate checks.
wget --no-check-certificate <url>
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn