2024-10-19
wget
’s Networking Prowesswget
isn’t just about fetching files; it’s a versatile tool for interacting with web servers and handling various network protocols. We’ll look at these capabilities with clear, practical examples.
Many servers require authentication before allowing downloads. wget
seamlessly handles this using the --user
and --password
options.
wget --user=username --password=password https://private-server.com/file.zip
Replace "username"
, "password"
, and the URL with your credentials and target file location. For enhanced security, consider using environment variables to store sensitive information instead of directly including them in the command.
export USERNAME="your_username"
export PASSWORD="your_password"
wget --user=$USERNAME --password=$PASSWORD https://private-server.com/file.zip
Working behind a proxy server? wget
can handle that too.
wget --proxy=http://proxy_server:port/ https://example.com/image.jpg
Replace "proxy_server:port"
with your proxy server address and port number. You can also specify different proxies for HTTP and HTTPS using --http-proxy
and --https-proxy
respectively. Authentication for the proxy can be added using --proxy-user
and --proxy-password
.
Network interruptions are inevitable. wget
’s ability to resume downloads is invaluable. This is enabled by default, but it’s good practice to explicitly specify it:
wget --continue https://large-file-server.com/large_file.iso
wget
will automatically detect the already downloaded portion and resume from where it left off.
wget -i
:Need to download a list of files? Create a text file (e.g., urls.txt
) containing each URL on a new line and use the -i
option:
https://example.com/file1.txt
https://example.com/file2.txt
https://example.com/file3.jpg
wget -i urls.txt
This efficiently downloads all files specified in urls.txt
.
Network conditions vary. wget
allows fine-grained control over timeouts and retries:
wget --timeout=15 --tries=3 https://unreliable-server.com/data.tar.gz
This command sets a 15-second timeout per attempt and tries the download up to 3 times before giving up.
wget
can recursively download an entire website, mirroring its structure locally. Use the -r
option (recursive) and -p
(get all necessary files, like images and CSS) for a detailed mirror.
wget -r -p -np -k https://example.com/
-np
prevents downloading files outside the specified directory, and -k
converts links to make the mirrored site work offline. This can be resource-intensive, so use caution before attempting this on large websites.
These examples showcase wget
’s power beyond simple downloads. Understanding these advanced features empowers you to manage network interactions efficiently and effectively within your Linux environment. Further exploration of wget
’s man page (man wget
) will reveal even more functionalities.