Can wget Execute Commands After Downloading?
This article explores whether the popular command-line utility
wget has a built-in mechanism to execute external scripts
or commands immediately after a file finishes downloading. While
wget itself lacks a direct “post-download execute” flag,
you will learn how to easily achieve this functionality using standard
command-line chaining techniques, shell scripts, and automation
tools.
Understanding Wget’s Direct Capabilities
The short answer is no; wget does not
possess a native command-line option (like --exec or
--on-completion) to trigger an external script or command
once a download wraps up. wget is strictly designed as a
network downloader. Its primary job is to fetch content via HTTP, HTTPS,
and FTP, log the progress, and save the data to your disk.
Because it adheres to the Unix philosophy of “doing one thing and doing it well,” it leaves task scheduling and process sequencing to the shell environment.
How to Execute Commands After Wget Finishes
Even though wget won’t run your scripts natively, you
can easily control the execution flow using your terminal’s shell
features. Here are the most common and effective ways to run a command
or script right after wget completes its task.
1. Sequential Execution (The
&& Operator)
If you want your command or script to run only if the
download succeeds, use the logical AND operator
(&&). This checks the exit status of
wget. If wget returns a success code (0), the
next command executes immediately.
wget https://example.com/file.zip && unzip file.zipIn this example, file.zip will extract only if it was
downloaded completely and without errors.
2. Unconditional Execution
(The ; Operator)
If you want your script to run regardless of whether the
download succeeded or failed, separate the commands with a
semicolon (;).
wget https://example.com/backup.tar.gz ; ./cleanup_script.shHere, cleanup_script.sh runs even if the network dropped
or the URL returned a 404 error.
3. Piping Directly to an Interpreter
For specific file types like shell scripts or installer scripts, you
can download the file and pass it directly to an interpreter using a
pipe (|). This executes the script without even saving it
to your hard drive first.
wget -O- https://example.com/install.sh | bashSecurity Note: Always verify the source of a script before piping it directly into
bash, as this can pose a significant security risk if the source is untrusted or compromised.
Advanced Automation with Shell Scripts
If you need to handle multiple downloads or perform complex logic
based on the downloaded files, wrapping wget inside a
wrapper script is the cleanest approach.
Below is an example of a simple Bash script that downloads a dataset, checks if the file exists, and then triggers a Python processing script:
#!/bin/bash
URL="https://example.com/data.csv"
OUTPUT="data.csv"
# Start the download
wget -O "$OUTPUT" "$URL"
# Check if the file was created successfully
if [ -f "$OUTPUT" ]; then
echo "Download complete. Starting data analysis..."
python3 analyze.py "$OUTPUT"
else
echo "Error: Download failed. Script aborted."
exit 1
fiAlternative Tools with Native Triggers
If your workflow absolutely requires a tool that manages its own post-download actions natively, you might want to consider alternative utilities:
- cURL: Like
wget,curlrelies on shell chaining (&&), but it handles stdout streaming more fluidly for piping into commands. - aria2: This is a lightweight, multi-protocol
download utility that does feature a native trigger. You can
use the
--on-download-completeoption to execute a specific hook or shell script automatically upon completion.