#!/bin/bash set -euo pipefail usage() { echo "Usage: $0 " echo " tar_root_dir Root directory to recursively search for .tar files" echo " max_processes Number of parallel decompression processes, must be a positive integer" echo " delete_tar_file Whether to delete tar files after decompression: 0=Keep 1=Delete" } if [ "$#" -ne 3 ]; then usage exit 1 fi TAR_ROOT_DIR="$1" MAX_PROCESSES="$2" DELETE_TAR_FILE="$3" if [ ! -d "${TAR_ROOT_DIR}" ]; then echo "Error: tar_root_dir does not exist or is not a directory: ${TAR_ROOT_DIR}" >&2 exit 1 fi if ! [[ "${MAX_PROCESSES}" =~ ^[1-9][0-9]*$ ]]; then echo "Error: max_processes must be a positive integer: ${MAX_PROCESSES}" >&2 exit 1 fi if ! [[ "${DELETE_TAR_FILE}" =~ ^[01]$ ]]; then echo "Error: delete_tar_file must be 0 or 1: ${DELETE_TAR_FILE}" >&2 exit 1 fi # Define global statistical variables total_tar_num=0 finished_tar_num=0 fail_tar_num=0 # ===================== Core Decompression Function ===================== unpack_tar() { local tar_file="$1" local tar_dir=$(dirname "${tar_file}") local tar_name=$(basename "${tar_file}") # Core logic: cd to the directory where the tar package is located to perform decompression and ensure correct path restoration cd "${tar_dir}" && tar xvf "${tar_name}" local unpack_status=$? if [ ${unpack_status} -eq 0 ]; then # Count +1 for successful decompression finished_tar_num=$((finished_tar_num+1)) local progress_percent=$(( finished_tar_num * 100 / total_tar_num )) echo -e "\rDecompression progress: [${finished_tar_num}/${total_tar_num}] ${progress_percent}% | Success: ${tar_file}" # Delete tar package after successful decompression if configured if [ ${DELETE_TAR_FILE} -eq 1 ]; then rm -f "${tar_file}" fi else # Count +1 for failed decompression fail_tar_num=$((fail_tar_num+1)) echo -e "\nDecompression failed: ${tar_file}" fi } # ===================== Step 1: Recursively find all tar packages and pre-count ===================== echo -e "Start recursively searching for all .tar files under [${TAR_ROOT_DIR}]..." # Recursively find all .tar files, sort by path, compatible with all path formats tar_files=() while IFS= read -r file; do tar_files+=("$file") done < <(find "${TAR_ROOT_DIR}" -type f -name "*.tar" | sort) # Count total number total_tar_num=${#tar_files[@]} if [ ${total_tar_num} -eq 0 ]; then echo -e "No .tar files found, script exit!" exit 0 fi echo -e "Search completed, total [${total_tar_num}] tar files found" echo -e "Start multi-process parallel decompression, process count: ${MAX_PROCESSES} | Delete tar files after decompression: $( [ ${DELETE_TAR_FILE} -eq 1 ] && echo "Yes" || echo "No" )" echo -e "==================================================\n" # ===================== Step 2: Multi-process parallel decompression + process limit ===================== for tar_file in "${tar_files[@]}"; do # Execute decompression function in background parallel unpack_tar "${tar_file}" & # Core rate limit: Wait for any background process to complete when the process count reaches the upper limit while (( $(jobs -p | wc -l) >= MAX_PROCESSES )); do wait -n done done # ===================== Final step: Wait for all background decompression processes to complete ===================== echo -e "\n\nWaiting for all remaining decompression processes to complete... (Remaining: $(jobs -p | wc -l) )" wait # ===================== Final decompression completion summary ===================== echo -e "\n==================================================" echo -e "All decompression tasks completed" echo -e "Decompression statistics: Total=${total_tar_num} | Success=${finished_tar_num} | Failed=${fail_tar_num}" echo -e "Decompressed files have been restored according to the original directory structure, no manual path adjustment required!" echo -e "=================================================="