Automatically Extract Multi-Volume RAR Files

Automating the extraction of multi-volume RAR archives saves time and eliminates the tedium of processing split files manually. Multi-part archives typically follow naming conventions such as .part01.rar or legacy .r00 series, and attempting to run an extraction command on every single file leads to redundant work and errors. This guide provides lightweight Bash and PowerShell scripts using the unrar command-line utility to automatically detect the initial volume of each set and extract the contents cleanly.

The Challenge with Multi-Volume Archives

When unrar processes the first archive in a multi-volume sequence, it automatically detects and extracts all subsequent parts in the sequence. If a script blindly runs unrar against every .rar file in a directory, the entire archive will be extracted repeatedly—once for every volume segment.

To script this correctly, the script must target only the first file of each multi-volume archive:

  • Modern naming: Files ending in .part1.rar, .part01.rar, or .part001.rar.
  • Legacy naming: The primary .rar file (accompanied by .r00, .r01, etc.).

Linux and macOS: Bash Script

The following Bash script uses find to locate the first volume of any modern multi-volume archive (as well as standalone .rar files that are not subsequent parts) and extracts them into their respective directories.

#!/usr/bin/env bash

# Set directory to the first argument, or default to current directory
TARGET_DIR="${1:-.}"

# Find and extract modern multi-volume archives (.part1.rar, .part01.rar, etc.)
find "$TARGET_DIR" -type f -regextype posix-extended -iregex '.*\.part0*1\.rar' | while IFS= read -r file; do
    echo "Extracting multi-volume archive: $file"
    unrar x -o- "$file" "$(dirname "$file")/"
done

# Find and extract legacy multi-volume archives (.rar alongside .r00)
find "$TARGET_DIR" -type f -name "*.rar" ! -name "*.part*.rar" | while IFS= read -r file; do
    base="${file%.rar}"
    if [[ -f "${base}.r00" ]]; then
        echo "Extracting legacy multi-volume archive: $file"
        unrar x -o- "$file" "$(dirname "$file")/"
    fi
done

Script Flags Explained:

  • x: Extracts files with their full relative directory paths intact.
  • -o-: Skip existing files to prevent overwriting already extracted data.
  • $(dirname "$file")/: Ensures extracted files are placed in the same folder as the archive.

Windows: PowerShell Script

On Windows, ensure unrar.exe (installed with WinRAR) is added to your system PATH. Open PowerShell and run the following script to search a folder recursively and process all multi-volume sets:

param (
    [string]$TargetFolder = "."
)

# Match modern multi-volume archives (e.g., .part01.rar, .part1.rar)
$modernParts = Get-ChildItem -Path $TargetFolder -Recurse -File | 
    Where-Object { $_.Name -match '\.part0*1\.rar$' }

foreach ($archive in $modernParts) {
    Write-Host "Extracting modern set: $($archive.FullName)"
    & unrar x -o- "$($archive.FullName)" "$($archive.DirectoryName)\"
}

# Match legacy multi-volume archives (base .rar with existing .r00)
$legacyParts = Get-ChildItem -Path $TargetFolder -Recurse -File -Filter "*.rar" | 
    Where-Object { $_.Name -notmatch '\.part\d+\.rar$' }

foreach ($archive in $legacyParts) {
    $r00Path = [System.IO.Path]::ChangeExtension($archive.FullName, ".r00")
    if (Test-Path $r00Path) {
        Write-Host "Extracting legacy set: $($archive.FullName)"
        & unrar x -o- "$($archive.FullName)" "$($archive.DirectoryName)\"
    }
}

Optional: Automatically Delete Archives After Extraction

If you want to automatically clean up disk space by deleting archive parts only after a successful extraction, check the exit code of unrar. In unrar, an exit code of 0 denotes success.

Bash Cleanup Snippet:

if unrar x -o- "$file" "$(dirname "$file")/"; then
    echo "Extraction successful. Deleting archive parts..."
    # Remove all parts matching the base name prefix
    base_prefix="${file%%.part*}"
    rm -f "${base_prefix}".part*.rar
fi