Commands.page Logo

How to Use Wget to Download and Handle Gzip Encoding on Ubuntu

This guide explains how to use the wget command-line utility on Ubuntu to download files from the web. It specifically covers handling scenarios where files are compressed using gzip encoding, ensuring you can retrieve and decompress data efficiently without manual intervention.

Install Wget

Most Ubuntu installations include wget by default. To verify it is installed, open your terminal and run the following command:

wget --version

If the command is not found, install it using the apt package manager:

sudo apt update
sudo apt install wget

Downloading the File

To download a file, use the wget command followed by the URL. If the file is gzip-compressed, wget will download the compressed version exactly as it exists on the server.

wget https://example.com/file.tar.gz

Wget automatically resumes broken downloads and follows redirects, making it reliable for large files.

Decompressing the Gzip File

Once the download is complete, you may need to decompress the file to access its contents. If the file has a .gz extension, use the gunzip command:

gunzip file.tar.gz

This will replace the compressed file with the uncompressed version. If you want to keep the original compressed file, use the -k flag:

gunzip -k file.tar.gz

Piping Directly to Decompression

For a faster workflow, you can download and decompress the file in a single step without saving the compressed version to your disk. This is done by piping the output of wget to gunzip:

wget -O - https://example.com/file.tar.gz | gunzip > file.tar

In this command, the -O - flag tells wget to output the downloaded content to standard output instead of a file, which is then passed directly to gunzip for decompression.