Commands.page Logo

Use aria2c to Download and Extract Automatically on Ubuntu

This guide explains how to configure the aria2c command-line utility on Ubuntu to download files and automatically extract them upon completion. By utilizing built-in hooks and simple shell commands, you can streamline your workflow and eliminate manual steps when handling compressed archives.

Install aria2

Begin by installing aria2 on your Ubuntu system using the package manager. Open your terminal and run the following command:

sudo apt update
sudo apt install aria2

Use the On-Download-Complete Hook

aria2c features a --on-download-complete option that executes a specified command after a download finishes. The path to the downloaded file is passed as an argument to the command. To download and extract a ZIP file immediately, use this command:

aria2c --on-download-complete="unzip %s" [URL]

Replace [URL] with the direct link to your file. The %s placeholder represents the full path of the downloaded file.

Handle Multiple Archive Formats

Since different archives require different extraction tools, you can use a bash conditional statement within the hook. This command checks the file extension and runs the appropriate extraction tool:

aria2c --on-download-complete="bash -c 'if [[ %s == *.zip ]]; then unzip %s; elif [[ %s == *.tar.gz ]]; then tar -xzvf %s; fi'" [URL]

This ensures that ZIP files are unpacked with unzip and GZIP tarballs are unpacked with tar.

Remove the Archive After Extraction

If you wish to save disk space by deleting the compressed file after extraction, add the remove command to your hook. The following example extracts a ZIP file and then deletes the original archive:

aria2c --on-download-complete="bash -c 'unzip %s && rm %s'" [URL]

Ensure you test these commands with non-critical files first to verify that the extraction logic matches your specific file types.