Commands.page Logo

How to Use aria2c to Download and Compress Files on Ubuntu

This guide demonstrates how to configure aria2c on Ubuntu to automatically compress downloaded files using a completion hook. It covers installing the utility, writing a simple shell script for compression, and executing downloads with the automated task attached. Following these steps allows you to save storage space and manage files efficiently without manual processing.

Install aria2

Open your terminal and install aria2 using the apt package manager.

sudo apt update
sudo apt install aria2

Create the Compression Script

You need a shell script that aria2c will execute once a download finishes. Create a new file named compress.sh in your home directory.

nano ~/compress.sh

Add the following content to the file. This script uses gzip to compress the file path passed by aria2c as the first argument.

#!/bin/bash
if [ -f "$1" ]; then
    gzip "$1"
fi

Save the file and exit the editor.

Make the Script Executable

Change the permissions of the script to allow execution.

chmod +x ~/compress.sh

Run aria2c with the Hook

Use the --on-completion flag to specify the path to your script. Replace the URL with your target file link.

aria2c --on-completion=/home/yourusername/compress.sh https://example.com/largefile.iso

When the download completes successfully, aria2c will trigger the script, and the file will be compressed to .gz format automatically.

Set the Hook Permanently

To avoid typing the flag every time, add the command to your aria2 configuration file. Open or create the config file at ~/.aria2/aria2.conf.

mkdir -p ~/.aria2
nano ~/.aria2/aria2.conf

Add the following line to the file:

on-completion=/home/yourusername/compress.sh

Now, every download initiated by aria2c on this system will automatically compress the finished file.