How to Use wget in Bash: Download Files Easily
Use
wget in bash to download files from the internet by typing wget [URL]. This command fetches the file at the given URL and saves it to your current directory.Syntax
The basic syntax of wget is simple and looks like this:
wget [options] [URL]
Here, URL is the web address of the file you want to download. options are extra settings you can add to control how wget works.
bash
wget [options] URL
Example
This example shows how to download a file from the internet using wget. It downloads the file and saves it in your current folder with the same name as on the server.
bash
wget https://example.com/sample.txtOutput
Saving to: ‘sample.txt’
sample.txt 100%[===================>] 1.23K --.-KB/s in 0s
2024-06-01 12:00:00 (12.3 MB/s) - ‘sample.txt’ saved [1260/1260]
Common Pitfalls
Some common mistakes when using wget include:
- Forgetting to include the full URL starting with
http://orhttps://. - Trying to download to a directory without write permission.
- Not using quotes around URLs with special characters.
Here is an example of a wrong and right way:
bash
# Wrong: missing protocol
wget example.com/file.txt
# Right: full URL with protocol
wget https://example.com/file.txtQuick Reference
| Option | Description |
|---|---|
| -O filename | Save the downloaded file with a custom name |
| -q | Run quietly without outputting download progress |
| -c | Continue a partially downloaded file |
| --limit-rate=amount | Limit download speed (e.g., 200k) |
| -r | Download files recursively from a website |
Key Takeaways
Use
wget [URL] to download files easily in bash.Always include the full URL starting with http:// or https://.
Use options like
-O to rename files or -c to resume downloads.Check permissions if downloads fail due to write errors.
Quote URLs with special characters to avoid errors.