If you’ve been working with Linux for some time, you know how to use wget command. Wget is a very handy tool that allows you to download files from the internet. If you see the error ‘wget command not found’ when you try to use this tool, you don’t have it installed.
In this article, I will show you how to install and use wget in the Linux operating systems and the commands for wget in Linux.
What is Wget?
The wget command is one of the most important utilities used on Linux systems, and it helps us download images, documents, PDF’s and much more from the web. Whether you’re using Linux or some other OS, you should know the wget command if you want to work with files from remote servers.
It supports HTTP, HTTPS, and FTP URLs. It also supports proxy connections for both FTP and HTTP. Wget works in the background, so you can leave it running and return to it afterward.
You can also start it at any time during your session. It is safe to let wget
be your default FTP client because wget
will not be confused when two login offers appear simultaneously.
How to Check if Wget is Installed?
If wget is installed, you can run it from the terminal. Run this command to check the version of wget
:
wget -V
Make sure that the wget package is installed on your system. In Ubuntu Linux or Debian, check if wget
is installed by entering the following into a terminal window:
which wget
It would help if you got the path to wget
the output. If not, wget is not installed in your system.
How to Install Wget on Ubuntu Linux or Debian?
To install wget on Ubuntu Linux or Debian, enter this command:
sudo apt-get install wget
After installation, check if wget
is installed by running the command in the above section again.
How to Install Wget on Mac?
To install wget on Ubuntu Linux or Debian, follow these steps:
- Install wget with Homebrew. If you don’t have Homebrew installed, follow my guide to installing Homebrew on a Mac.
- Once Homebrew is installed, run this command:
brew install wget
How to Install Wget on Windows?
Wget is not natively available on Windows, but you can install it by following these steps:
- Download the latest version of Wget from the official GNU website.
- Extract the downloaded file to a location on your computer (e.g., C:\Program Files\GnuWin32).
- Add the location of wget.exe to your system’s PATH environment variable.
- Open a new Command Prompt window and type “wget” to verify the installation.
How to Download Wget From the Internet?
If you’re not able to install wget
using the method above, you can install it by downloading it by running this command:
curl -O https://ftp.gnu.org/gnu/wget/wget-latest.tar.gz
Once installed, extract the files in the gzip file:
tar zxvf wget-latest.tar.gz
After the files have been extracted, a new folder is created. Go into this folder, and run these commands to install wget:
$ cd wget-XXX
$ ./configure
$ make
$ sudo make install
After installation, you can follow the section above to verify wget
is installed.
Fundamentals of Using Wget to Download Files
This section will explore the fundamentals of using wget
to download files from the command line. We will examine the structure of wget commands and key options that are essential to understand.
Wget Syntax
Wget has two arguments [OPTION] and [URL].
wget [OPTION] ... [URL] ...
In wget
, the option argument refers to the various command arguments you can pass to wget
to customize its behavior.
The url
argument, conversely, refers to the Uniform Resource Locator of the file you want to download. This can be a link to a webpage or a direct link to a specific file.
Together, the option
and url
arguments are used to provide instructions to wget
on how to download a file. For example, you might use the -r
option to recursively download all files linked to from a webpage, and specify the URL of the webpage you want to download with the url
argument.
Here’s an example command that uses both the option
and url
arguments:
wget -r https://techcolleague.com
In this case, the -r
option tells wget
to recursively download all files linked from the https://techcolleague.com
website. The https://techcolleague.com
URL is specified as the url
argument.
Related: Wget vs Curl: What’s the Difference?
Wget Help Commands
To see a list of available wget
commands, you can use the wget
help command by running:
wget -h
This will display a comprehensive list of all wget
command parameters.
Wget Usage Examples
To use the wget
command to download files from the internet, here are a few examples:
- Download a single file:
wget https://ftp.gnu.org/gnu/wget/wget2-latest.tar.gz
- Download a file to a specific directory:
wget -P /home/tim/Downloads https://ftp.gnu.org/gnu/wget/wget2-latest.tar.gz
The argument -P specifies the directory prefix. In this case, wget
saves the downloaded file wget2-latest.tar.gz into the folder /home/tim/Downloads.
- Download a file with a specific name:
wget -O wget.tar.gz https://ftp.gnu.org/gnu/wget/wget2-latest.tar.gz
The argument -O is used to specify the output file. In this case, wget saves the output of the downloaded file wget2-latest.tar.gz as wget.tar.gz.
How to Use Wget With Python?
Yes, you can use wget
with Python. There are several ways to do this, but one common approach is to use the subprocess
module to call the wget
command from within a Python script.
Here is an example Python script that uses wget
to download a file:
import subprocess
url = 'http://myserver.com/myfile.zip'
output_directory = '/path/to/output/directory'
subprocess.call(['wget', url, '-P', output_directory])
This script imports the subprocess
module and defines two variables: url
, which is the URL of the file to download, and output_directory
, which is the directory where the downloaded file should be saved.
The script then uses the subprocess.call()
function to call the wget
command with the appropriate parameters to download the file and save it to the specified output directory.
Note that when using subprocess
to call external commands like wget
, it’s essential to be cautious of potential security risks. Always sanitize any user input to prevent executing arbitrary commands or shell injections.
How to Use Proxies With Wget?
To use proxies with wget
, you can use the --e
option followed by the proxy server URL. For example, if your proxy server is located at http://proxy.myserver.com:8080
, you can use the following command to download a file through that proxy:
wget -e use_proxy=yes -e http_proxy=http://proxy.myserver.com:8080 http://myserver.com/myfile.zip
Frequently Asked Questions
Can you Use Wget With Python?
Yes it is possible to use wget
with Python. One common approach is to use the subprocess
module to call the wget
command from within a Python script. This allows you to download files using wget
programmatically within your Python code.
However, when using subprocess
to call external commands like wget
, it’s important to be cautious of potential security risks and sanitize any user input to prevent executing arbitrary commands or shell injection.
Is Wget Free?
Yes, wget
is free and open-source software released under the GNU General Public License (GPL) version 3 or later. This means that it can be used, modified, and distributed freely by anyone as long as they comply with the terms of the GPL license.
Because wget
is open-source software, its source code is also freely available for anyone to view and modify.
What is Wget Used For?
wget
is a command-line tool used for downloading files from the internet. It supports various protocols such as HTTP, HTTPS, and FTP. It is commonly used to automate downloading large files or entire websites.
Conclusion
Wget is an efficient command-line utility that allows users to download files from the internet quickly and easily. Its many customization options allow users to tailor its behavior to suit their needs.
To use wget, first, you have to install it on your system, and then you can use the wget command followed by the URL of the file you want to download. If you require further help, the –help option of wget will give you more information and usage examples.