0
0
Bash-scriptingHow-ToBeginner · 3 min read

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.txt
Output
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:// or https://.
  • 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.txt
📊

Quick Reference

OptionDescription
-O filenameSave the downloaded file with a custom name
-qRun quietly without outputting download progress
-cContinue a partially downloaded file
--limit-rate=amountLimit download speed (e.g., 200k)
-rDownload 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.