Recursive Unrar Command for Nested Archives

This guide explains how to recursively extract archives located inside another archive using the unrar utility. While unrar natively reconstructs subdirectories stored within a single archive, unpacking nested archives (archives inside archives) requires combining unrar with shell automation to process files as they are unpacked.

Standard Directory Extraction vs. Nested Archives

To extract a single RAR file while preserving its internal folder structure, use the x command:

unrar x archive.rar

The x command recursively restores the stored folder paths. However, unrar does not have a built-in parameter to detect and automatically unpack additional archive files (such as .rar or .zip files) that were packed inside the parent archive.

Recursively Extracting Nested RAR Files

To extract an archive and repeatedly unpack any nested archives it contains, combine unrar with a shell loop.

Method 1: The find While Loop (Linux / macOS)

Navigate to the directory containing your main archive and run the following command:

unrar x archive.rar && while [ $(find . -type f -name "*.rar" | wc -l) -gt 0 ]; do
    find . -type f -name "*.rar" -execdir unrar x -o+ {} \; -exec rm -f {} \;
done

How It Works:

  1. unrar x archive.rar: Extracts the root archive and maintains its original folder tree.
  2. while [ $(find . -type f -name "*.rar" | wc -l) -gt 0 ]: Continuously checks whether any .rar files remain in the working directory or any subfolders.
  3. find . -type f -name "*.rar": Searches the entire directory tree for newly extracted RAR files.
  4. -execdir unrar x -o+ {} \;: Runs unrar x directly inside the subdirectory where the nested archive is found. The -o+ switch automatically overwrites existing files to prevent interactive prompt hangs.
  5. -exec rm -f {} \;: Removes the processed archive after extraction to prevent infinite loops.

Non-Destructive Extraction (Keeping Original Archives)

If you must keep the compressed nested archives instead of deleting them, track extracted files using a log rather than deleting them:

unrar x archive.rar
touch extracted.log
while IFS= read -r archive; do
    unrar x -o- "$archive" "$(dirname "$archive")"
    echo "$archive" >> extracted.log
done < <(find . -type f -name "*.rar" ! -exec grep -Fxq "{}" extracted.log \; -print)

This ensures every nested archive is discovered and extracted in-place without repeatedly processing previously unpacked files.