Skip to content

Instantly share code, notes, and snippets.

@rflpazini
Last active September 23, 2025 18:58
Show Gist options
  • Save rflpazini/7360605e2978974005fe3667541f0cc0 to your computer and use it in GitHub Desktop.
Save rflpazini/7360605e2978974005fe3667541f0cc0 to your computer and use it in GitHub Desktop.
Unzip script used to extract .zip files from a folder at once
#!/bin/bash
# --- Smart Script to Unzip Files ---
# 1. Initial setup to find the script's own location.
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SOURCE_FOLDER="$SCRIPT_DIR/../ISO"
# Check if the source directory exists.
if [ ! -d "$SOURCE_FOLDER" ]; then
echo "Error: Directory '$SOURCE_FOLDER' not found. πŸ˜•"
exit 1
fi
# 2. Find all .zip files and store them in an array.
# This allows us to count them before processing.
mapfile -d '' files < <(find "$SOURCE_FOLDER" -maxdepth 1 -type f -name "*.zip" -print0)
total_files=${#files[@]}
# Exit if no zip files were found.
if [ "$total_files" -eq 0 ]; then
echo "No .zip files found in '$SOURCE_FOLDER'."
exit 0
fi
# 3. Print the initial "to-do" list.
echo "Files to Unzip:"
for zipfile in "${files[@]}"; do
echo " [ ] $(basename "$zipfile")"
done
# 4. Move the terminal cursor up to the start of the list we just printed.
# 'tput cuu' means "cursor up".
tput cuu "$total_files"
# 5. Process each file and update its line in the list.
for zipfile in "${files[@]}"; do
output_dir="${zipfile%.zip}"
mkdir -p "$output_dir"
# Unzip the file silently to avoid cluttering the visual list.
unzip -oq "$zipfile" -d "$output_dir" > /dev/null 2>&1
# 'tput el' clears the current line from the cursor to the end.
# The 'echo' then overwrites '[ ]' with 'βœ…' and moves the cursor down one line.
tput el
echo " βœ… $(basename "$zipfile")"
done
echo "---"
echo "All files processed successfully! πŸŽ‰"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment