The wget command in Linux is a powerful tool for downloading files from the web. It supports HTTP, HTTPS, and FTP protocols and offers features like recursive downloads, resuming interrupted downloads, and background downloads. Below are practical examples to master wget:
Contents
- Download a Single File
- Download and Save with a Different Name
- Download in the Background
- Resume an Interrupted Download
- Download Multiple Files
- Download Files from a List
- Limit Download Speed
- Download an Entire Website (Mirror)
- Download Files via FTP
- Download with Authentication
- Retry Failed Downloads
- Set a Custom User-Agent
- Download Files Matching a Pattern
- Exclude Specific File Types
- Download with a Timeout
Download a Single File
wget https://example.com/file.zip
Downloads file.zip from the specified URL.
Download and Save with a Different Name
wget -O custom_name.zip https://example.com/file.zip
-O: Saves the file ascustom_name.zip.
Download in the Background
wget -b https://example.com/large_file.iso
-b: Runs the download in the background.
Resume an Interrupted Download
wget -c https://example.com/large_file.iso
-c: Continues downloading a partially downloaded file.
Download Multiple Files
wget https://example.com/file1.zip https://example.com/file2.zip
Downloads multiple files in sequence.
Download Files from a List
wget -i file_list.txt
-i: Reads URLs fromfile_list.txtand downloads them.
Limit Download Speed
wget --limit-rate=200k https://example.com/large_file.iso
--limit-rate=200k: Limits the download speed to 200 KB/s.
Download an Entire Website (Mirror)
wget --mirror --convert-links https://example.com
--mirror: Recursively downloads the entire website.--convert-links: Converts links for local viewing.
Download Files via FTP
wget ftp://example.com/file.zip
Downloads file.zip from an FTP server.
Download with Authentication
wget --user=username --password=password https://example.com/file.zip
--userand--password: Authenticate with the server.
Retry Failed Downloads
wget --tries=5 https://example.com/file.zip
--tries=5: Retries the download up to 5 times.
Set a Custom User-Agent
wget --user-agent="Mozilla/5.0" https://example.com/file.zip
--user-agent: Mimics a browser user-agent.
Download Files Matching a Pattern
wget -r -A "*.jpg" https://example.com/images/
-r: Recursively downloads.-A "*.jpg": Downloads only.jpgfiles.
Exclude Specific File Types
wget -r -R "*.mp4" https://example.com/media/
-R "*.mp4": Excludes.mp4files from the download.
Download with a Timeout
wget --timeout=10 https://example.com/file.zip
--timeout=10: Sets a 10-second timeout for the connection.
Key Notes:
- Resume Downloads: Always use
-cfor large files to avoid restarting from scratch. - Recursive Downloads: Use
-ror--mirrorfor downloading entire directories or websites. - Permissions: Ensure you have permission to download files from the target server.
