Last active
July 24, 2025 10:42
-
-
Save metaphore/570902782ca82c7d21b77738df029475 to your computer and use it in GitHub Desktop.
Unzip only wav files from all the archives in the current dir and report skipped files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| BOLD="\e[1m" | |
| RESET="\e[0m" | |
| RED="\e[31m" | |
| total_wav=0 | |
| for zipfile in *.zip; do | |
| [ -e "$zipfile" ] || continue | |
| zipname="${zipfile%.zip}" | |
| mkdir -p "$zipname" | |
| # List all files inside the zip | |
| all_files=$(unzip -Z1 "$zipfile") | |
| # Files to extract (only *.wav) | |
| wav_files=$(echo "$all_files" | grep -i '\.wav$') | |
| # Files skipped (non-wav) | |
| skipped_files=$(comm -23 <(echo "$all_files" | sort) <(echo "$wav_files" | sort)) | |
| # Report skipped files if any | |
| if [[ -n "$skipped_files" ]]; then | |
| echo -e "📦 ${BOLD}${zipname}${RESET}" | |
| while IFS= read -r f; do | |
| echo -e " 🚫 ${RED}$f${RESET}" | |
| done <<< "$skipped_files" | |
| fi | |
| # Extract only wav files | |
| if [[ -n "$wav_files" ]]; then | |
| unzip -q "$zipfile" '*.wav' -d "$zipname" || { | |
| echo -e "❌ Failed to extract .wav files from ${BOLD}$zipfile${RESET}" | |
| continue | |
| } | |
| else | |
| echo -e "📦 ${BOLD}${zipname}${RESET} has no .wav files to extract." | |
| # Clean up and continue to next zip | |
| rmdir "$zipname" 2>/dev/null || rm -r "$zipname" | |
| continue | |
| fi | |
| # Rename and copy wav files | |
| shopt -s nullglob | |
| for file in "$zipname"/*; do | |
| [ -f "$file" ] || continue | |
| filename="$(basename "$file")" | |
| newname="${zipname}_${filename}" | |
| mv "$file" "$zipname/$newname" | |
| cp "$zipname/$newname" ./ | |
| ((total_wav++)) | |
| done | |
| shopt -u nullglob | |
| # Clean up extraction directory | |
| rmdir "$zipname" 2>/dev/null || rm -r "$zipname" | |
| done | |
| echo -e "\n✅ ${BOLD}${total_wav}${RESET} .wav file(s) extracted and copied." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment