Linux wget Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux wget command to download files from the internet. The wget command is a powerful tool that allows you to download files from the web, including web pages, images, and other types of content. You will start by understanding the purpose and syntax of the wget command, including common options and example usage. Then, you will learn how to download files from the internet using wget, and how to automate file downloads with wget scripting.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/PackagesandSoftwaresGroup -.-> linux/curl("`URL Data Transferring`") linux/PackagesandSoftwaresGroup -.-> linux/wget("`Non-interactive Downloading`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") subgraph Lab Skills linux/curl -.-> lab-423004{{"`Linux wget Command with Practical Examples`"}} linux/wget -.-> lab-423004{{"`Linux wget Command with Practical Examples`"}} linux/cd -.-> lab-423004{{"`Linux wget Command with Practical Examples`"}} linux/mkdir -.-> lab-423004{{"`Linux wget Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the wget Command

In this step, you will learn about the purpose and syntax of the wget command in Linux. The wget command is a powerful tool used to download files from the internet.

First, let's understand the basic syntax of the wget command:

wget [options] [URL]

The most common options used with wget are:

  • -O or --output-document=FILE: Save the downloaded file with a different name.
  • -P or --directory-prefix=PREFIX: Save the downloaded file in the specified directory.
  • -c or --continue: Continue a previously interrupted download.
  • -r or --recursive: Download files recursively, including directories and subdirectories.
  • -b or --background: Run wget in the background.

Example usage:

wget https://example.com/file.zip

This will download the file file.zip from the URL https://example.com/file.zip and save it in the current directory.

Example output:

--2023-04-11 10:00:00--  https://example.com/file.zip
Resolving example.com (example.com)... 93.184.216.34
Connecting to example.com (example.com)|93.184.216.34|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12345678 (12M) [application/zip]
Saving to: 'file.zip'

file.zip            100%[===================>]  12.35M  3.32MB/s    in 3.7s

2023-04-11 10:00:04 (3.32 MB/s) - 'file.zip' saved [12345678/12345678]

The output shows the download progress, the file size, and the time taken to complete the download.

Download Files from the Internet Using wget

In this step, you will learn how to use the wget command to download files from the internet.

Let's start by downloading a file from a website:

wget https://example.com/file.zip

This will download the file.zip file from the https://example.com/file.zip URL and save it in the current directory.

Example output:

--2023-04-11 10:00:00--  https://example.com/file.zip
Resolving example.com (example.com)... 93.184.216.34
Connecting to example.com (example.com)|93.184.216.34|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12345678 (12M) [application/zip]
Saving to: 'file.zip'

file.zip            100%[===================>]  12.35M  3.32MB/s    in 3.7s

2023-04-11 10:00:04 (3.32 MB/s) - 'file.zip' saved [12345678/12345678]

You can also save the downloaded file with a different name using the -O or --output-document option:

wget -O myfile.zip https://example.com/file.zip

This will download the same file but save it as myfile.zip instead of file.zip.

If you want to download a file to a specific directory, use the -P or --directory-prefix option:

wget -P ~/downloads https://example.com/file.zip

This will download the file and save it in the ~/downloads directory.

Automate File Downloads with wget Scripting

In this step, you will learn how to use wget in a script to automate file downloads.

First, let's create a simple script to download multiple files:

#!/bin/bash

## URLs to download
urls=(
  "https://example.com/file1.zip"
  "https://example.com/file2.tar.gz"
  "https://example.com/file3.pdf"
)

## Download each file
for url in "${urls[@]}"; do
  wget "$url"
done

Save this script as download_files.sh and make it executable:

chmod +x download_files.sh

Now, you can run the script to download the files:

./download_files.sh

This will download the three files specified in the urls array.

You can also add options to the wget command within the script. For example, to save the files in a specific directory:

#!/bin/bash

## Download directory
download_dir="~/downloads"

## URLs to download
urls=(
  "https://example.com/file1.zip"
  "https://example.com/file2.tar.gz"
  "https://example.com/file3.pdf"
)

## Create the download directory if it doesn't exist
mkdir -p "$download_dir"

## Download each file
for url in "${urls[@]}"; do
  wget -P "$download_dir" "$url"
done

This script will create the ~/downloads directory (if it doesn't already exist) and download the files to that directory.

Summary

In this lab, you learned about the purpose and syntax of the wget command in Linux, which is a powerful tool used to download files from the internet. You explored the common options used with wget, such as -O to save the downloaded file with a different name, -P to save the file in a specified directory, -c to continue a previously interrupted download, -r to download files recursively, and -b to run wget in the background. You also learned how to use wget to download files from the internet, including specifying the URL and observing the download progress, file size, and time taken to complete the download.

You then learned how to automate file downloads with wget scripting, which allows you to schedule and manage multiple file downloads more efficiently.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like