| #!/bin/bash |
| set -euo pipefail |
|
|
| usage() { |
| echo "Usage: $0 <tar_root_dir> <max_processes> <delete_tar_file>" |
| 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 |
|
|
| |
| total_tar_num=0 |
| finished_tar_num=0 |
| fail_tar_num=0 |
|
|
| |
| unpack_tar() { |
| local tar_file="$1" |
| local tar_dir=$(dirname "${tar_file}") |
| local tar_name=$(basename "${tar_file}") |
|
|
| |
| cd "${tar_dir}" && tar xvf "${tar_name}" |
| local unpack_status=$? |
|
|
| if [ ${unpack_status} -eq 0 ]; then |
| |
| 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}" |
| |
| |
| if [ ${DELETE_TAR_FILE} -eq 1 ]; then |
| rm -f "${tar_file}" |
| fi |
| else |
| |
| fail_tar_num=$((fail_tar_num+1)) |
| echo -e "\nDecompression failed: ${tar_file}" |
| fi |
| } |
|
|
| |
| echo -e "Start recursively searching for all .tar files under [${TAR_ROOT_DIR}]..." |
| |
| tar_files=() |
| while IFS= read -r file; do |
| tar_files+=("$file") |
| done < <(find "${TAR_ROOT_DIR}" -type f -name "*.tar" | sort) |
|
|
| |
| 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" |
|
|
| |
| for tar_file in "${tar_files[@]}"; do |
| |
| unpack_tar "${tar_file}" & |
|
|
| |
| while (( $(jobs -p | wc -l) >= MAX_PROCESSES )); do |
| wait -n |
| done |
| done |
|
|
| |
| echo -e "\n\nWaiting for all remaining decompression processes to complete... (Remaining: $(jobs -p | wc -l) )" |
| wait |
|
|
| |
| 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 "==================================================" |
|
|