How to use wget on command line
Wget is a powerful command line tool for downloading files from the internet. It’s a lightweight, easy-to-use tool that’s perfect for downloading single files or entire websites. In this post, we’ll cover the basics of using Wget, including how to install it, the general syntax, and how to perform common tasks such as downloading a file, resuming a download, and limiting download speed.
Installing wget
Wget is available for most operating systems, including Linux, macOS, and Windows. On Linux, you can usually install Wget using your package manager.
Install wget on Ubuntu/Debian
On Ubuntu or Debian systems, you can run the following command to install Wget:
sudo apt-get install wget
Install wget on macOS
On macOS, you can install Wget using Homebrew:
brew install wget
Install wget on Windows
On Windows, you can download the executable from the Wget website.
General Syntax of wget
The general syntax for using wget is as follows:
wget [options] [URL]
The options can be used to customize the behavior of Wget. Some common options include -O to specify the output file name, -q to suppress output, and -nc to skip downloads of files that already exist. The URL specifies the location of the file or website to download.
How to Download a File with Wget
To download a file with Wget, simply provide the URL of the file as an argument:
wget https://example.com/file.txt
This will download the file to the current directory with the same name as the original file.
Downloading a File to a Specific Directory
If you want to download a file to a specific directory, you can use the -P option:
wget -P /path/to/directory https://example.com/file.txt
This command will download the file to the specified directory.
Limiting the Download Speed
If you want to limit the download speed, you can use the –limit-rate option. For example, to limit the download speed to 1 megabyte per second, you can use the following command:
wget --limit-rate=1m https://example.com/file.txt
Resuming a Download
If a download is interrupted, you can use the -c option to resume the download from where it left off:
wget -c https://example.com/file.txt
Downloading in Background
To download a file in the background, you can use the -b option:
wget -b https://example.com/file.txt
This will start the download in the background and return control to the terminal.
Changing the Wget User-Agent
To change the user-agent that Wget uses when making requests, you can use the –user-agent option. For example, to set the user-agent to “Mozilla/5.0”, you can use the following command:
wget --user-agent="Mozilla/5.0" https://example.com/file.txt
Downloading via FTP
To download a file via FTP, you can use the –ftp-user and –ftp-password options to provide the login credentials:
wget --ftp-user=username --ftp-password=password ftp://example.com/file.txt
Downloading to the Standard Output
To download a file to the standard output, you can use the -O - option:
wget -O - https://example.com/file.txt
This will print the contents of the file to the terminal.
Conclusion
Wget is a versatile and powerful command line tool for downloading files from the internet. It’s easy to use and can handle a wide range of download scenarios. Whether you’re downloading a single file or an entire website, Wget has the tools you need to get the job done quickly and efficiently.
In this post, we covered the basics of using Wget, including installing it on different operating systems, the general syntax for using it, and how to perform common tasks such as downloading a file, limiting download speed, and resuming a download.
With Wget, you can easily automate and script downloads, making it a powerful tool for developers and power users. Whether you’re downloading files for personal or professional use, Wget is a reliable and efficient tool that can help you get the job done quickly and easily.