Bash Script to Unrar and Delete All Archives

Managing large collections of compressed files manually can be tedious and time-consuming. This guide provides a simple, robust Bash script to automatically extract every .rar archive within a target directory and safely delete the original archive file only after a successful extraction.

Prerequisites

Before running the script, ensure you have the unrar utility installed on your Linux system.

On Ubuntu/Debian:

sudo apt update && sudo apt install unrar

On Fedora/CentOS/RHEL:

sudo dnf install unrar

On Arch Linux:

sudo pacman -S unrar

The Bash Script

Create a new file named extract_and_remove.sh and add the following code:

#!/bin/bash

# Target directory (defaults to current directory if not specified)
TARGET_DIR="${1:-.}"

# Check if target directory exists
if [ ! -d "$TARGET_DIR" ]; then
    echo "Error: Directory '$TARGET_DIR' does not exist."
    exit 1
fi

# Loop through all .rar files in the specified directory
shopt -s nullglob
for archive in "$TARGET_DIR"/*.rar; do
    # Skip multi-part archives other than part 1 to avoid duplicate extractions
    if [[ "$archive" =~ \.part[0-9]+\.rar ]] && [[ ! "$archive" =~ \.part0*1\.rar ]]; then
        continue
    fi

    echo "Extracting: $archive"

    # Extract the archive into the same directory
    unrar x -o+ "$archive" "$TARGET_DIR"

    # Delete the archive only if unrar succeeded (exit code 0)
    if [ $? -eq 0 ]; then
        echo "Extraction successful. Deleting original: $archive"
        
        # If it's a multi-part archive, remove all parts
        if [[ "$archive" =~ \.part0*1\.rar ]]; then
            prefix="${archive%.part0*1.rar}"
            rm -f "$prefix".part*.rar
        else
            rm -f "$archive"
        fi
    else
        echo "Error: Failed to extract $archive. Original file kept."
    fi
done

echo "Process completed."

How It Works

  • TARGET_DIR="${1:-.}": Sets the search path to the argument passed when executing the script. If no argument is passed, it defaults to the current directory (.).
  • shopt -s nullglob: Prevents the loop from running with the literal string *.rar if no RAR files are found.
  • unrar x -o+: Extracts files with full paths preserved (x) and automatically overwrites existing files (-o+).
  • if [ $? -eq 0 ]: Checks the exit status of the unrar command. The original file is removed with rm only if unrar returns 0, preventing data loss if an archive is corrupted or password-protected.
  • Multi-part handling: Detects multi-part archives (e.g., .part1.rar, .part2.rar) to extract them from the first part only and cleans up all associated parts once finished.

Running the Script

  1. Make the script executable:
chmod +x extract_and_remove.sh
  1. Run the script in your current directory:
./extract_and_remove.sh

Or pass a specific folder path:

./extract_and_remove.sh /path/to/your/archives

Quick One-Liner Alternative

If you do not need multi-part handling and prefer a fast terminal command, navigate to your target folder and run:

for f in *.rar; do unrar x -o+ "$f" && rm "$f"; done

This sequentially extracts each archive and runs rm "$f" only if unrar exits successfully.