The wget command in Linux

Downloading files from the internet is something we do many times in our day-to-day life. However, we do it through a web browser or through programs. How about doing it from the terminal? For this, we will use the wget command in Linux.

What is the wget command?

According to the wget command website

GNU Wget is a free software package for retrieving files using HTTP, HTTPS, FTP and FTPS, the most widely used Internet protocols. It is a non-interactive commandline tool, so it may easily be called from scripts, cron jobs, terminals without X-Windows support, etc.

In this way, the tool helps us to download files via the Internet quickly and easily. Despite being a tool that is used via the terminal, it is elementary and powerful.

As you might expect, the wget command is available for almost all Linux distributions out there. So, we will be able to use it in almost any server or desktop environment.

Let’s go for it.

Install the wget command on your Linux distribution

Usually, the wget command is installed by default on every distribution. But it is always a good idea to be certain.

If you are using Debian, Ubuntu, Linux Mint, Elementary OS or any distribution of this family, it is most likely already installed, but you can run

sudo apt install wget

Regarding RHEL, Fedora, Rocky Linux and Alma Linux you have to run the following command

sudo dnf install wget

Or if you use OpenSUSE

sudo zypper in wget

Finally, for Arch Linux, Manjaro and the rest of the distributions of this family,

sudo pacman -S wget

Then, you can run

wget --version
GNU Wget 1.20.3 built on linux-gnu.

To check the installed version to show that wget is installed correctly.

Using the wget command

The basic syntax of the command is as follows

wget [option] [URL]

Therefore, we can download any file using this syntax. For example, to download NodeJS in version 16 we can execute.

wget https://nodejs.org/dist/v16.13.2/node-v16.13.2-linux-x64.tar.gz

This will download the file in the directory where we are located.

Moreover, we will be able to download several files at the same time

wget [URL1] [URL2] [URL3]

for example,

wget https://nodejs.org/dist/v16.13.2/node-v16.13.2-linux-x64.tar.gz https://nodejs.org/dist/v15.6.0/node-v15.6.0-linux-x64.tar.gz

In this way, both files will be downloaded.

An interesting option is -b which makes wget run in the background. Quite useful for scripts and scheduled tasks.

wget -b https://nodejs.org/dist/v15.6.0/node-v15.6.0-linux-x64.tar.gz

You will get a screen output similar to this

Continuing in background, pid 24563.
Output will be written to 'wget-log.1'.

In addition to this, we can limit the bandwidth wget can use in a download. To complete this, just use the --limit-rate option followed by a value. Example:

wget --limit-rate=500K https://nodejs.org/dist/v15.6.0/node-v15.6.0-linux-x64.tar.gz

In the above example, I have limited the download rate to no more than 500Kb.

By default, when wget downloads files it will save them with the name they have, but you can with the -O option set it to save them with a different name.

For example:

wget -O node.tar.gz https://nodejs.org/dist/v15.6.0/node-v15.6.0-linux-x64.tar.gz

So instead of node-v15.6.0-linux-x64.tar.gz it will be saved with the name node.tar.gz.

If you have an unstable internet connection or in case something unexpected happens, you can resume a download. To complete this, you need to download with the -c option in the command.

wget -c https://nodejs.org/dist/v15.6.0/node-v15.6.0-linux-x64.tar.gz

For you to resume the download, the server has to accept it.

Moreover, you can save the file in a different folder thanks to the -P option

wget -P ~/nodejs https://nodejs.org/dist/v15.6.0/node-v15.6.0-linux-x64.tar.gz

This will save the file directly to the specified directory.

If you are going to use wget to download files using FTP, you can run

wget --ftp-user=USER --ftp-password=password ftp://ftpserver.com/file.tar

Where you set the user and password, as well as the exact path to the file.

This way, you can use the wget command in Linux.

Conclusion

Downloading files from the internet is something basic in a current system, but doing it from the terminal may seem complicated, but the reality is that thanks to the wget command it is simple.

I hope you liked the post, and you can share it with your friends.

Leave a Comment

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

Scroll to Top