wget Command in Linux (Syntax, Examples, Cheat Sheet + Interview Questions)

wget Command in Linux (Syntax, Examples, Cheat Sheet + Interview Questions)

Looking for wget command syntax, examples, or interview questions?

This guide covers everything from basic wget usage to advanced options, including a quick cheat sheet, real-world scenarios, and commonly asked interview questions to help you master wget in Linux.


wget command - Quick Cheat Sheet

CommandDescription
wget URLDownload a file from URL
wget -O file URLSave downloaded file with custom name
wget -P /path URLDownload file to specific directory
wget -c URLResume interrupted download
wget -b URLDownload file in background
wget -q URLQuiet mode (hide output)
wget --show-progress URLShow detailed progress
wget --limit-rate=1m URLLimit download speed
wget -i file.txtDownload multiple URLs from file
wget -m URLMirror entire website
wget -r URLRecursive download
wget -np -r URLRecursive download without parent directory
wget --level=2 -r URLLimit recursive depth
wget -t 10 URLRetry download 10 times
wget --retry-connrefused URLRetry on connection refused
wget --timeout=30 URLSet timeout for download
wget --user=user --password=pass URLDownload with authentication
wget --ftp-user=user --ftp-password=pass URLFTP download with credentials
wget --http-user=user --http-password=pass URLHTTP authentication
wget -R "file" -r URLReject specific files during download
wget -A ".zip" -r URLAccept only specific file types
wget -nd URLDo not create directories
wget -nH URLDisable host directory creation
wget --cut-dirs=2 URLSkip directories while downloading
wget -e robots=off URLIgnore robots.txt restrictions
wget --no-check-certificate URLIgnore SSL certificate errors
wget URL && unzip file.zipDownload and unzip in one command
wget --spider URLCheck if URL is accessible (no download)
wget --mirror --convert-links --adjust-extension URLFull website mirror with usable links
wget --background --tries=inf URLInfinite retry in background
wget --user-agent="Mozilla" URLSet custom user agent
wget --header="Authorization: Bearer TOKEN" URLAdd custom headers
wget -o log.txt URLSave logs to file
wget -S URLShow server response headers
wget --dns-timeout=30 URLSet DNS timeout
wget --connect-timeout=30 URLSet connection timeout

wget command syntax

The basic syntax of the wget command in Linux is:

text
wget [options] [URL]
  • wget → command used to download files from the web
  • [options] → flags to control behavior (output, retries, speed, etc.)
  • [URL] → the file or website location to download

Example:

text
wget https://example.com/file.zip

This downloads the file to the current directory.


wget command examples

Download a single file using wget

Downloads file to current directory.

text
wget https://example.com/file.zip

Download file with custom name (-O)

text
wget -O myfile.zip https://example.com/file.zip

Saves file as myfile.zip.

Download to specific directory (-P)

Downloads file into specified directory.

text
wget -P /home/user/downloads https://example.com/file.zip

Download multiple files using list (-i)

Create a file urls.txt:

text
https://example.com/file1.zip
https://example.com/file2.zip

Run:

text
wget -i urls.txt

Downloads all files listed.

Download files silently (hide progress)

text
wget -q https://example.com/file.zip
  • -q → quiet mode (no output)

For logging instead:

text
wget -o log.txt https://example.com/file.zip

Combine wget with unzip in one command

text
wget https://example.com/file.zip && unzip file.zip
  • Downloads file
  • Immediately extracts it

Alternative (auto naming):

text
wget -O temp.zip https://example.com/file.zip && unzip temp.zip

wget in Linux (Real-world usage scenarios)

Resume interrupted downloads (wget retry and -c)

If a download is interrupted due to network issues, you can resume it using the -c option:

text
wget -c https://example.com/largefile.zip
  • Continues downloading from where it stopped
  • Useful for large files or unstable connections

Limit download speed and bandwidth control

To avoid consuming full bandwidth, limit the download speed:

text
wget --limit-rate=500k https://example.com/file.zip
  • 500k → limits speed to 500 KB/s
  • Can also use m for MB (e.g., 2m)

Run wget in background (nohup, &)

Run downloads in the background so they continue after terminal is closed:

Using &:

text
wget https://example.com/file.zip &

Using nohup:

text
nohup wget https://example.com/file.zip &
  • nohup ensures process continues even after logout
  • Output is saved in nohup.out

Retry failed downloads automatically

By default, wget retries failed downloads. You can customize retries:

text
wget --tries=10 https://example.com/file.zip

Set infinite retries:

text
wget --tries=0 https://example.com/file.zip

Add delay between retries:

text
wget --wait=5 --tries=10 https://example.com/file.zip

Download files with timestamping (avoid duplicates)

Avoid re-downloading unchanged files using timestamping:

text
wget -N https://example.com/file.zip
  • Downloads only if remote file is newer
  • Useful for syncing files or scripts

Advanced wget usage and options

Recursive download and website mirroring

Download entire websites using recursive mode:

text
wget -r https://example.com

For full mirroring:

text
wget -m https://example.com
  • -r → recursive download
  • -m → mirror mode (includes recursion + timestamping)

Limit recursion depth and file types

Control how deep wget should crawl:

text
wget -r -l 2 https://example.com
  • -l 2 → limit depth to 2 levels

Download only specific file types:

text
wget -r -A .pdf https://example.com
  • -A → accept only matching extensions

Download entire directory or website

Download a directory listing:

text
wget -r -np https://example.com/files/
  • -np → no parent (stay within directory)

Exclude specific files and directories

Exclude unwanted file types:

text
wget -r -R .jpg,.png https://example.com

Exclude directories:

text
wget -r --exclude-directories=images,css https://example.com

Control directory structure and output

Avoid creating nested directories:

text
wget -r -nd https://example.com
  • -nd → no directories (flat structure)

Customize directory structure:

text
wget -r --cut-dirs=2 https://example.com/path/to/files/
  • Removes first 2 directory levels

Save all files to a specific directory:

text
wget -r -P /home/user/downloads https://example.com

wget command with authentication and security

Download files with username and password

You can authenticate while downloading files using HTTP/FTP credentials:

text
wget --user=username --password=password https://example.com/file.zip

For FTP:

text
wget ftp://username:password@example.com/file.zip

For better security (avoid exposing password in history), use:

text
wget --ask-password --user=username https://example.com/file.zip

Use wget with cookies and session handling

Useful for downloading content behind login sessions.

Save cookies:

text
wget --save-cookies cookies.txt --post-data "username=user&password=pass" https://example.com/login

Use cookies:

text
wget --load-cookies cookies.txt https://example.com/protected-file.zip

Keep session cookies:

text
wget --keep-session-cookies --save-cookies cookies.txt https://example.com

Ignore SSL certificate errors (when needed)

If SSL certificate validation fails:

text
wget --no-check-certificate https://example.com/file.zip
  • Useful for self-signed certificates
  • Not recommended for production (security risk)

Use wget with headers and tokens

Add custom headers (e.g., API tokens):

text
wget --header="Authorization: Bearer YOUR_TOKEN" https://api.example.com/data

Multiple headers:

text
wget --header="Header1: value1" --header="Header2: value2" https://example.com

wget configuration and customization

Understanding wgetrc configuration file

wgetrc is a configuration file used to define default settings.

Example:

text
user_agent = MyCustomAgent
wait = 5
limit_rate = 200k

This avoids repeating options in every command.

Global vs user wgetrc settings

  • Global config:
text
/etc/wgetrc
  • User config:
text
~/.wgetrc

User config overrides global settings.

Customize default behavior of wget

Example customizations in ~/.wgetrc:

text
continue = on
tries = 5
timeout = 30

Now wget will:

  • resume downloads automatically
  • retry 5 times
  • timeout after 30 seconds

wget vs curl

Key differences between wget and curl

Featurewgetcurl
Primary useDownload filesTransfer data
Recursive downloadYesNo
Resume supportYesYes
Protocol supportHTTP, HTTPS, FTPMany (HTTP, FTP, SCP, etc.)
Ease of useSimpleMore flexible

When to use wget vs curl

Use wget when:

  • Downloading files
  • Mirroring websites
  • Automating downloads

Use curl when:

  • Calling APIs
  • Sending POST/PUT requests
  • Debugging HTTP requests

Performance and use case comparison

  • wget is optimized for bulk downloads and automation
  • curl is optimized for data transfer and API interaction

Example (API request using curl):

text
curl -X GET https://api.example.com/data

Example (file download using wget):

text
wget https://example.com/file.zip

wget interview questions and answers

Basic wget interview questions

1. What is wget in Linux?
wget is a command-line utility used to download files from the internet using protocols like HTTP, HTTPS, and FTP.

2. What is the basic syntax of wget?

text
wget [options] [URL]

3. How do you download a file using wget?

text
wget https://example.com/file.zip

4. Which option is used to resume downloads?

text
wget -c https://example.com/file.zip

5. How do you download a file silently?

text
wget -q https://example.com/file.zip

Intermediate wget questions with answers

1. How do you download multiple files using wget?

text
wget -i urls.txt

2. How do you save a file with a different name?

text
wget -O newname.zip https://example.com/file.zip

3. How do you limit download speed?

text
wget --limit-rate=500k https://example.com/file.zip

4. How do you retry failed downloads?

text
wget --tries=10 https://example.com/file.zip

5. How do you download files in the background?

text
wget https://example.com/file.zip &

Advanced wget scenario-based questions

1. How do you mirror an entire website?

text
wget -m https://example.com

2. How do you download only specific file types (e.g., PDFs)?

text
wget -r -A .pdf https://example.com

3. How do you exclude certain file types?

text
wget -r -R .jpg,.png https://example.com

4. How do you use authentication with wget?

text
wget --user=username --password=password https://example.com/file.zip

5. How do you pass headers (e.g., API tokens)?

text
wget --header="Authorization: Bearer TOKEN" https://api.example.com/data

Real-world troubleshooting questions

1. Why is wget failing with SSL error?
Use:

text
wget --no-check-certificate https://example.com

2. Why is wget not resuming download?
Ensure:

  • Server supports partial downloads
  • Use -c option

3. Why is download very slow?
Possible reasons:

  • Server-side throttling
  • Network issues
  • Use --limit-rate or check bandwidth

4. How to debug wget issues?

text
wget -d https://example.com/file.zip

5. How to handle redirects properly?
Wget follows redirects by default, but ensure URL is correct and accessible.


Frequently Asked Questions

1. What is wget command in Linux?

wget is a command-line tool used to download files from the internet using HTTP, HTTPS, and FTP protocols.

2. What is the basic syntax of wget?

The basic syntax is wget [options] URL, where options control download behavior.

3. How to download a file using wget?

Use wget URL to download a file directly from the given link.

4. How to resume download using wget?

Use wget -c URL to continue a partially downloaded file.

5. What are common wget options?

Common options include -O (output file), -c (resume), -q (quiet mode), and --limit-rate to control speed.

Conclusion

The wget command is one of the most powerful tools in Linux for downloading files, automating tasks, and handling large-scale data transfers. From basic file downloads to advanced use cases like website mirroring and authenticated requests, wget provides flexibility and reliability. By understanding its syntax, options, and real-world scenarios, you can significantly improve your productivity in Linux environments.


Official Documentation

For complete reference and advanced usage, refer to the official GNU documentation:

You can also access documentation directly from your system using man wget.

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.