How to Use wget Command in Linux: Basic Syntax and Examples
The
wget command in Linux downloads files from the internet using a URL. You run it by typing wget [options] URL in the terminal, which saves the file to your current folder.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 commands you can add to change how wget works, like saving the file with a different name or downloading multiple files.
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
--2024-06-01 12:00:00-- https://example.com/sample.txt
Resolving example.com (example.com)... 93.184.216.34
Connecting to example.com (example.com)|93.184.216.34|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1256 (1.2K) [text/plain]
Saving to: ‘sample.txt’
sample.txt 100%[===================>] 1.23K --.-KB/s in 0s
2024-06-01 12:00:01 (12.3 MB/s) - ‘sample.txt’ saved [1256/1256]
Common Pitfalls
Some common mistakes when using wget include:
- Not having internet connection or wrong URL causes download failure.
- Running
wgetwithout permissions to write in the folder will fail. - For large files, not using options like
-cto continue interrupted downloads can waste time.
Here is a wrong and right way to continue a download:
bash
# Wrong way: restart download from beginning wget https://example.com/largefile.zip # Right way: continue interrupted download wget -c https://example.com/largefile.zip
Quick Reference
| Option | Description |
|---|---|
| -O filename | Save download as 'filename' instead of original name |
| -c | Continue a partially downloaded file |
| -q | Quiet mode, no output |
| -r | Download files recursively from a website |
| --limit-rate=amount | Limit download speed (e.g., 100k, 1m) |
Key Takeaways
Use
wget URL to download files simply from the terminal.Add
-c to continue interrupted downloads without starting over.Use
-O filename to save the file with a custom name.Check your internet connection and folder permissions if downloads fail.
Explore options like
-r for recursive downloads and --limit-rate to control speed.