How to Limit Wget Redirections When Downloading on Ubuntu
This article provides a concise guide on using the wget command in Ubuntu to manage file downloads with specific redirection limits. It covers the essential syntax for setting the maximum number of HTTP redirects allowed, ensuring secure and controlled retrieval of files from web servers without following infinite chains.
Using the Max Redirect Flag
By default, wget follows HTTP redirects automatically. To prevent the
tool from getting stuck in a redirect loop or following too many hops,
you can use the --max-redirect option. This flag accepts an
integer value that defines the maximum number of redirections wget will
follow before aborting the download.
Command Syntax and Example
To specify the limit, append the flag and your desired number to the standard wget command. For example, to allow a maximum of three redirections while downloading a file, run the following command in your terminal:
wget --max-redirect=3 https://example.com/file.zipIf the server attempts to redirect the request a fourth time, wget will stop the process and display an error message indicating that the maximum number of redirects was exceeded.
Verifying Redirects with Verbose Mode
When troubleshooting download issues, it is helpful to see exactly
where the redirects are occurring. You can combine the redirection limit
with the verbose flag -v to print detailed progress
information to the screen.
wget -v --max-redirect=5 https://example.com/file.zipThis command shows each step of the redirection process while enforcing the safety limit. Using these options ensures your download sessions on Ubuntu remain efficient and secure against misconfigured servers.