The wget command is an indispensable utility for any Linux user or system administrator, designed for non-interactive downloading of files from the web. This guide will equip you with the precise knowledge to leverage wget effectively, enabling you to download files, directories, or even entire websites directly from the command line. Understanding wget is crucial for scripting, automating tasks, and efficiently managing file acquisition on remote servers where a graphical browser is unavailable or impractical. By the end of this Tutorial, you will be proficient in its core functionalities, from simple file downloads to more complex recursive operations and download management.
Prerequisites
Before proceeding, ensure you have:
- Access to a Linux-based operating system (e.g., Ubuntu, CentOS, Fedora).
- A terminal or command-line interface.
- Basic familiarity with executing commands in the terminal.
- Sudo privileges for installing packages, if
wgetis not already present.
Step 1: Verify wget Installation
It is imperative to confirm that wget is installed on your system. Without it, none of the subsequent operations will be possible. Execute the following command to check its presence:
wget --version
If wget is installed, this command will display its version information. If it reports “command not found” or a similar error, you must install it. The installation process varies slightly depending on your distribution:
-
For Debian/Ubuntu-based systems:
sudo apt update sudo apt install wget -
For RHEL/CentOS/Fedora-based systems:
sudo dnf install wget # For Fedora 22+ and RHEL 8+ sudo yum install wget # For older CentOS/RHEL versions
Pro-Tip:
Always run sudo apt update or sudo dnf update/sudo yum update before installing new packages to ensure your package lists are current. This prevents potential dependency issues.
Step 2: Perform a Basic File Download
The most fundamental use of wget is to download a single file. You simply provide the URL of the file you wish to retrieve. The file will be saved in your current working directory under its original filename.
wget https://example.com/path/to/yourfile.zip
Upon execution, wget will display progress information, including the download percentage, speed, and estimated time remaining. Once complete, yourfile.zip will be in the directory from which you ran the command.
Warning:
Be acutely aware of your current working directory. Files downloaded with this basic command will land exactly where you are, potentially cluttering your home directory if not managed properly. Use pwd to confirm your location.
Step 3: Download to a Specific Directory
To maintain an organized filesystem, it is often necessary to download files directly into a designated directory. The -P or --directory-prefix option allows you to specify the target location.
wget -P /home/user/downloads https://example.com/path/to/anotherfile.pdf
This command will download anotherfile.pdf into the /home/user/downloads directory. If the specified directory does not exist, wget will create it.
Practical Tip:
Always create your target directories with mkdir -p /path/to/directory before initiating complex downloads, even though wget can create them. This preemptive step ensures permissions and structure are as intended.
Step 4: Rename Downloaded Files
Sometimes the remote filename is unsuitable, or you need to avoid conflicts with existing local files. The -O or --output-document option allows you to specify a new name for the downloaded file.
wget -O custom_name.tar.gz https://example.com/archive.tar.gz
This will download archive.tar.gz but save it locally as custom_name.tar.gz.
Warning:
The -O option is powerful but dangerous. If custom_name.tar.gz already exists in the target directory, wget will overwrite it without any prompt or warning. Exercise extreme caution to prevent unintended data loss.
Step 5: Resume Incomplete Downloads
Network interruptions or system crashes can leave large downloads incomplete. Critically, wget can resume these partial downloads, saving significant time and bandwidth. Use the -c or --continue option.
wget -c https://example.com/very_large_file.iso
If very_large_file.iso exists partially in the current directory, wget will detect it and continue the download from where it left off. If the file is not present, it will start a fresh download.
Pro-Tip:
This functionality is indispensable for large files or when operating over unstable network connections. Always include -c when re-attempting a download that might have been interrupted.
Step 6: Download in Background (Non-interactive)
For very large files or when you need to close your terminal session, downloading in the background is essential. The -b or --background option detaches wget from the terminal.
wget -b https://example.com/huge_dataset.zip
wget will print a message indicating that it is starting in the background and will continue downloading even if you log out. The progress and output will be redirected to a file named wget-log in the current directory.
Practical Tip:
After initiating a background download, monitor its progress by tailing the log file: tail -f wget-log. For more robust background execution, especially for scripts, consider combining wget with nohup and `&` (e.g., nohup wget [URL] &).
Step 7: Limit Download Speed
To prevent wget from saturating your network connection, you can impose a speed limit using the --limit-rate option. This is particularly useful in shared network environments.
wget --limit-rate=500k https://example.com/another_large_file.bin
This command will cap the download speed at 500 kilobytes per second. You can specify rates in bytes, kilobytes (k), or megabytes (m).
Use Case:
Limiting the rate is critical when downloading multiple large files concurrently or when running other bandwidth-sensitive applications, ensuring fair resource allocation across your network.
Step 8: Download Multiple Files from a List
When faced with the task of downloading numerous files, manually entering each URL is inefficient and prone to error. wget can read a list of URLs from a local file using the -i or --input-file option.
First, create a text file (e.g., urls.txt) with one URL per line:
https://example.com/file1.txt
https://example.com/image.jpg
https://example.com/document.pdf
Then, execute wget with the input file:
wget -i urls.txt
wget will process each URL in the file sequentially.
Pro-Tip:
Combine this with -P to download all files from the list into a specific directory, and -b for background operation, creating a powerful bulk download solution.
Step 9: Download Entire Websites (Recursive Download)
For archiving, offline browsing, or mirroring, wget can recursively download an entire website or a section of it. This is a powerful feature but requires careful use.
wget -r -l 2 --no-parent --convert-links https://example.com/docs/
-r(--recursive): Turns on recursive retrieval.-l 2(--level=2): Specifies the maximum depth of recursion (e.g., 2 levels deep from the starting URL).--no-parent: Preventswgetfrom traversing up to parent directories.--convert-links: After downloading, convert links in the HTML documents to point to local files for offline browsing.
Warning:
Recursive downloads can consume vast amounts of disk space and bandwidth rapidly. Always start with a shallow recursion level (e.g., -l 1 or -l 2) and use --no-parent to avoid downloading the entire internet. Be respectful of server resources and check the website’s robots.txt before attempting extensive crawls.
To further enhance your wget proficiency, delve into its extensive man page (man wget) to uncover options for proxy settings, authentication, sending specific HTTP headers, and more complex filtering rules. These advanced capabilities transform wget from a simple downloader into a comprehensive command-line web client.
