How to Use the wget Command to Download Files in Ubuntu

When you are managing an Ubuntu server via the command line, you do not have access to a graphical web browser like Google Chrome or Mozilla Firefox. If you need to download a software package, a script, or an ISO image from the internet directly to your server, you need a command-line downloader. The most ubiquitous and reliable tool for this job is wget.

wget (Web Get) is a non-interactive network downloader that supports HTTP, HTTPS, and FTP protocols. Because it is non-interactive, it can run completely in the background, making it perfect for automated scripts and large downloads that might take hours to complete.

How to Perform a Basic Download

The simplest way to use wget is to simply type the command followed by the direct URL of the file you want to download.

wget https://example.com/software-package.tar.gz

When you press Enter, wget will connect to the server, begin downloading the file into your current working directory, and display a progress bar showing the download speed and estimated time of completion.

How to Rename a File During Download

Sometimes, download links are messy. If you download a file from a dynamic link like https://example.com/download.php?id=12345, the file will be saved on your hard drive with the confusing name download.php?id=12345.

To tell wget to save the file under a specific name, use the -O (capital O for Output) flag:

wget -O installer.zip https://example.com/download.php?id=12345

This command downloads the file from that messy URL but neatly saves it as installer.zip on your machine.

How to Resume an Interrupted Download

If you are downloading a massive 4GB database file and your SSH connection drops or your local network fails at 90%, you do not have to start from the beginning. wget has a built-in resume feature.

Navigate to the directory containing the partially downloaded file and use the -c (continue) flag, followed by the exact same URL you used previously:

wget -c https://example.com/massive-database.sql.gz

wget will inspect the local file, check the remote server, and seamlessly resume the download exactly where it left off.

How to Download Files in the Background

If a download is going to take several hours, you cannot close your terminal window, or the download will instantly cancel. To solve this, you can tell wget to detach from the terminal and run quietly in the background using the -b (background) flag.

wget -b https://example.com/huge-file.iso

The terminal will immediately return control to you so you can continue working. wget will automatically create a log file named wget-log in your current directory. You can check the progress of your background download at any time by reading the end of that log file using the tail command:

tail -f wget-log

How to Download Multiple Files from a List

If you need to download 50 different images or PDF documents, running the wget command 50 separate times is incredibly inefficient. Instead, you can create a simple text document containing a list of URLs.

  1. Use a text editor like nano to create a file named urls.txt.
  2. Paste all your download links into the file, ensuring there is only one URL per line. Save and exit.
  3. Run wget using the -i (input file) flag:
wget -i urls.txt

wget will systematically read the text file from top to bottom, downloading every single file in the list sequentially without any further input required from you.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.