How to Install and Use wget on CentOS

In this tutorial, we’re going to show you how to install and use wget on CentOS. We’ll include useful and practical examples of the wget command.

This tutorial will work on CentOS Stream, CentOS 8, RHEL, AlmaLinux, Rocky Linux, and others.

All the wget command examples will work on all distros, including Ubuntu and Debian.

How to fix the “-bash: /usr/bin/wget: No such file or directory” error

In some cases, wget may already be installed on your CentOS system. If it’s not installed, you’ll get the “no such file or directory” error. To fix it, you just need to install wget. Follow the instructions below to install wget.

How to install wget on CentOS

You can install wget with this single command:

sudo yum install wget -y

And now you can start using it without getting an error.

Tip: if you’re using Fedora or a newer version of RHEL/CentOS, just replace “yum” with “dnf”, if using Ubuntu/Debian, replace “yum” with “apt”

How to use wget on CentOS (basic)

The basic syntax of the wget command is:

wget [OPTIONS] [URL]

Here’s a basic example:

wget https://linuxstans.com/wp-content/uploads/2020/10/LinuxStansIcon512.png

This command will download the PNG file to the directory you’re currently in.

If the URL contains special characters ( * ? ] [ ), then you just need to put the URL in quotes. So as a safety measure, you can always put the URLs in quotes when using wget, regardless if they have a special character or not.

We’ll show you more specific useful examples below.

How to save the file with a different name

When downloading a file, you can save it under a different file name. To do that, you should use the “-O” option:

wget -O logo.png "https://linuxstans.com/wp-content/uploads/2020/10/LinuxStansIcon512.png"

This command will save the “LinuxStansIcon512.png” image as “logo.png”

How to save the file in a different directory

If you don’t want to download the file in the directory you’re currently in, you can use the “-P” option to specify a different directory. For example:

wget -P /var/www/logos/ https://linuxstans.com/wp-content/uploads/2020/10/LinuxStansIcon512.png

This will download the “LinuxStansIcon512.png” image to the /var/www/logos directory.

How to download a full website

You can also use the wget command to download a full website. To do that, you should use the “-m” option:

wget -m https://linuxstans.com

This command will download the whole Linux Stans website. This is useful if you often use your computer offline, and still want to read whatever a website has published. The -m option creates a mirror of the website you specify.

How to run the downloading in the background

If it’s a larger file, the downloading may take longer. So to run the wget downloading process in the background and continue using your system, you need to use the “-b” option. One example is when downloading a full website. So you can combine both options like:

wget -m -b https://linuxstans.com

This command will download the full website in the background.

How to use wildcards to download all files in a certain FTP directory

One useful example when using the wget command to download FTP files is using a wildcard. For example, to download all PNGs from an FTP directory, you can use the * wildcard, like below:

wget ftp://linuxstans.com/logos/*.png

This will download all PNG files from the logos folder from the LinuxStans FTP server.

How to download password-protected files

Some files may have password protection. To download them, you need to include the HTTP username and password in the wget command, like so:

wget --user=linuxstans--ask-password https://linuxstans.com/wp-content/uploads/2024/03/ProtectedFile.zip

This command will use “linuxstans” as the username and you will be prompted to enter the password after you enter the wget command. You can also enter the password directly in the wget command, but it’s less secure because the password will be visible to other users.

How to download multiple files from a .txt file

If you had a text file with multiple URLs (one URL per line), for example:

cat urls.txt

https://linuxstans.com/wp-content/uploads/2024/03/file1.zip
https://linuxstans.com/wp-content/uploads/2024/03/file2.png
https://linuxstans.com/wp-content/uploads/2024/03/file3.jpg
https://linuxstans.com/wp-content/uploads/2021/07/randomFile.txt

You can use the “-i” option to download all the files from that text file, like this:

wget -i urls.txt

How to download multiple files within wget

If you don’t want to use a .txt file and you have multiple URLs you want to download at once, you can use \ to separate them, for example:

wget https://linuxstans.com/wp-content/uploads/2024/03/file1.zip \ 
https://linuxstans.com/wp-content/uploads/2024/03/file2.png \ 
https://linuxstans.com/wp-content/uploads/2024/03/file3.jpg \
https://linuxstans.com/wp-content/uploads/2021/07/randomFile.txt

This will also download all 4 files.

And more!

There are many more useful examples of how to use the wget command. You can go through the help page to get an idea of what other options are available:

wget -help

How do you use wget? What’s a useful and practical example you can share?

Leave a comment

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