June 30, 2024
Tutorials

Wget Command in Linux with Examples

Wget command in Linux with examples

Wget is a command-line tool mainly used in Linux systems. It is primarily used to fetch content or files from web servers. Wget provides various options, including -r: recursive downloading, -i: input file reading, and more, for customization.

Additionally, wget is known for its simplicity and user-friendliness, thus achieving popularity.

This technical writing will show you the practical use of wget command in Linux. All the commands have been executed on Ubuntu 24.04 LTS.

Wget Command in Linux with Examples?

This detailed guide will cover:

  • How to Install the “wget” on a Linux-based System?
  • How to Utilize the “wget” tool in Linux Systems?

How to Install the “wget” on a Linux-based System?

Utilize these steps to install “wget”, a command tool, on your Linux system, including Ubuntu 24.04.

Step 1: Update your Linux Files

To ensure that your Linux system files are up to date, execute this common and simple command:

sudo apt update

When your Linux system has been updated, you can proceed to the next step.

Step 2: Install the “wget”

To utilize the wget command, first, you need to install the wget package using the APT package manager:

sudo apt install wget -y

If wget is already installed on your Linux system, the terminal will display the installed version, such as 1.21.4.

How to Utilize the “wget” tool in Linux Systems?

Following are some examples to demonstrate the use of the “wget” command in Linux systems.

Create a Directory to Store Files

First, you need to create a directory specifically to store your examples and navigate into it with the command:

mkdir wget_examples && cd wget_examples

You can see that the “wget_examples” directory has been created and at the same time navigated into it.

1: Download a File via wget

It is very easy to utilize the wget for downloading a single file from any URL. For example, run the command to download a file from “greenwebpage.com”:

wget https://greenwebpage.com/community/how-to-install-docker-on-ubuntu-24-04/

You can confirm the contents of the directory to see the downloaded file:

ls

With the “ls” command, you will see the downloaded file in your local directory.

2: Save a file with Your Preferred Name

You can utilize the wget command to download and save file(s) with a distinct name:

wget -O docker-install-guide.html https://greenwebpage.com/community/how-to-install-docker-on-ubuntu-24-04/

As demonstrated above, you have successfully renamed the file, such as “docker-install-guide.html”

Instead of the default file name while downloading.

3: Download File(s) in the Background:

Wget command facilitates you to download the file(s) from the internet in the background to not disturb you. Simply, add the “-b” option with the wget command in the following manner:

wget -b https://greenwebpage.com/community/how-to-install-docker-on-ubuntu-24-04/

After the downloading process has finished, you can verify it from the “wget-log” file:

cat wget-log

As presented above, you can see the detailed status of the downloaded file.

4: Download to a Particular Directory

With the addition of the “-P” option following by the path of the directory, the wget command will download the file to the particular folder, such as “~/wget_examples”:

wget -P ~/wget_examples https://greenwebpage.com/community/how-to-install-docker-on-ubuntu-24-04/

There you are, the file has been downloaded to the “~/wget_examples” directory.

5: Download Multiple Selected Files

Wget enables you to download multiple files. For the purpose, create a file such as “selected_urls.txt”:

nano selected_urls.txt

Copy the links or urls that you want to download:

#Docker installation guide 
How to Install Docker on Ubuntu 24.04
#Laravel installation guide
How to Install Laravel on Ubuntu 24.04
#Redis installation guide https://greenwebpage.com/community/how-to-install-redis-on-ubuntu-22-04/

Once you have copied all the urls in the text file (i.e. selected_urls.txt), save the file.

Utilize the following command to download all files listed in selected_urls.txt:

wget -i selected_urls.txt
 

You can verify the selected file downloaded using the selected_urls.txt:

ls

The above three files, encircled with a red box, can be seen in your local directory (i.e. wget_examples).

6: Download File(s) Recursively

If you want download files recursively, you can easily utilize the “wget” command in the following way:

wget -r https://greenwebpage.com/community/
 

In the above output, you can inspect that the command downloaded all the content of the website.

To verify what is downloaded inside the “greenwebpage.com”, run the command:

du -ah greenwebpage.com
 

You can see the list of the files that have been downloaded along with the size of the “greenwebpage.com” directory.

Conclusion

Wget is a commonly used powerful tool for downloading files from the internet in Linux systems. By adding various useful options (i.e., -b, -r, -i, -O, -P), you can enhance the use of the wget command. The wget command can be used efficiently to manage your downloads.

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video