Last active
February 20, 2025 13:48
-
-
Save kristijorgji/c144561595d9c9e08cbc2d7f327e1194 to your computer and use it in GitHub Desktop.
zip_folders.sh
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 | |
#=============================================================================== | |
# π¦ Folder Zipper Script | |
#=============================================================================== | |
# This script zips all directories inside a given source directory and stores | |
# the zipped files inside a specified destination directory. | |
# | |
# USAGE: | |
# ./zip_folders.sh <sourceDir> <destDir> | |
# | |
# PARAMETERS: | |
# sourceDir - The directory containing subfolders to zip. (Required) | |
# destDir - The directory where zipped files will be stored. (Required) | |
# | |
# REQUIREMENTS: | |
# - zip command installed | |
# | |
# INSTALLATION: | |
# macOS: | |
# brew install zip | |
# | |
# Linux (Debian/Ubuntu): | |
# sudo apt install zip -y | |
# | |
# Linux (Arch): | |
# sudo pacman -S zip | |
# | |
# Linux (Fedora): | |
# sudo dnf install zip | |
# | |
# AUTHOR: | |
# Kristi Jorgji (@kristijorgji on GitHub) | |
#=============================================================================== | |
# Ensure both arguments are provided | |
if [[ -z "$1" || -z "$2" ]]; then | |
echo "β Error: Missing arguments." | |
echo "Usage: ./zip_folders.sh <sourceDir> <destDir>" | |
exit 1 | |
fi | |
# Assign arguments to variables | |
SOURCE_DIR="$1" | |
DEST_DIR="$2" | |
# Ensure source directory exists | |
if [[ ! -d "$SOURCE_DIR" ]]; then | |
echo "β Error: Source directory '$SOURCE_DIR' does not exist." | |
exit 1 | |
fi | |
# Create destination directory if it doesn't exist | |
mkdir -p "$DEST_DIR" | |
# Count total folders to zip | |
total_folders=$(find "$SOURCE_DIR" -mindepth 1 -maxdepth 1 -type d | wc -l) | |
zip_count=0 # Initialize zipped folder counter | |
echo "π¦ Found $total_folders folders to zip at $SOURCE_DIR" | |
echo "------------------------------------------" | |
# Iterate through all directories inside the source directory | |
for folder in "$SOURCE_DIR"/*/; do | |
if [[ -d "$folder" ]]; then # Ensure it's a directory | |
folder_name=$(basename "$folder") # Extract folder name | |
zip_file="$DEST_DIR/$folder_name.zip" # Destination zip file path | |
zip_count=$((zip_count + 1)) | |
echo "[π $zip_count/$total_folders] Zipping '$folder_name'..." | |
# Change into the source directory and zip only the folder name | |
(cd "$SOURCE_DIR" && zip -r "$zip_file" "$folder_name" >/dev/null) | |
echo "β Done: $zip_file" | |
fi | |
done | |
echo "π All $zip_count folders have been successfully zipped!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment