Introduction
In the Linux environment the terminal becomes an extension of the user allowing tasks to be executed quickly and without depending on graphical environments. Among the most notable tools for obtaining files from the internet is wget a non-interactive download program that supports the HTTP HTTPS and FTP protocols. Its popularity stems from its simplicity its robustness and the large number of options that allow adapting the download to almost any scenario. In this article we will see from the most basic syntax to advanced tricks that will make your work with wget more efficient.
Basic Syntax
To start a download it is enough to type wget followed by the URL of the resource. For example:
wget https://ejemplo.com/documento.pdf
When executing that line the program will connect to the server request the file and save it in the current directory keeping the original name. If the server redirects the request wget will follow the redirection automatically. In case a file with the same name exists wget will overwrite it unless otherwise indicated by some option.
Most Used Options
- -o file : saves the message log to the indicated file instead of printing it to the terminal.
- -O file : defines the name of the output file overriding the default name.
- -c : continues a download that has been previously stopped reusing the already downloaded part.
- -r : activates recursive mode allowing downloading of entire directories or websites.
- -l number : sets the maximum recursion level when combined with -r.
- -k : converts links in the downloaded documents to work locally.
- -p : downloads all necessary resources to correctly display an HTML page such as images style sheets and scripts.
- -U agent : specifies a custom user agent useful when some servers block generic requests.
- –limit-rate=speed : restricts the bandwidth used by the download accepting values such as 200k or 1,5m.
- –wait=seconds : introduces a pause between successive requests to reduce load on the remote server.
- –retry-connrefused : tells wget to keep trying if the connection is initially refused.
Practical Examples
- Download a file and rename it when saving:
wget -O informe_final.pdf https://ejemplo.com/informe.pdf - Resume a large download that was interrupted by a power outage:
wget -c https://ejemplo.com/copia_de_seguridad.iso - Obtain a complete website for offline browsing:
wget -r -l 2 -k -p https://ejemplo.com/blog/ - Limit speed to 250 KB/s to avoid saturating the link while working:
wget --limit-rate=250k https://ejemplo.com/actualizacion.zip - Change the user agent to simulate a modern browser:
wget -U NavegadorWeb https://ejemplo.com/detalles.html - Download only PDF files from a directory:
wget -r -l 1 -A pdf https://ejemplo.com/documentos/ - Attempt reconnection up to five times before giving up:
wget --tries=5 https://ejemplo.com/archivo_inestable.gz
Tips and Best Practices
- Always verify the URL before running wget to avoid accidental downloads of unwanted content.
- When using recursive mode combine -l with -np to avoid ascending to parent directories.
- Use the log file (-o) to troubleshoot connection or authentication problems.
- If you need authentication incorporate the –user and –password flags or better yet use a .netseguro file to avoid leaving credentials in the history.
- For scheduled downloads combine wget with cron or systemd timers.
- Remember that wget does not interpret JavaScript so some modern sites may require additional tools such as curl or wget with specific headers.
Limitations and Considerations
- Wget does not process JavaScript so sites that depend on scripts to load content may require tools such as curl or headless browsers.
- Some servers limit the number of simultaneous connections; using too many requests with -r can lead to blocks.
- For very large downloads it is advisable to use –continue and verify integrity with checksums.
Conclusion
Wget remains one of the most reliable and versatile tools for downloading files from the Linux terminal. Its wide variety of options allows it to adapt from a simple capture of a file to the complete cloning of a website for offline use. Mastering its syntax and knowing the most used options will save you time and give you greater control over your downloads. Now that you have the basic concepts and several practical examples you are ready to incorporate wget into your daily workflow.
This post is also available in ESPAÑOL.