Skip to content

Instantly share code, notes, and snippets.

@u1i
Created May 24, 2025 09:16
Show Gist options
  • Save u1i/1cace0edcde5bf18c057d2f367a0b2b9 to your computer and use it in GitHub Desktop.
Save u1i/1cace0edcde5bf18c057d2f367a0b2b9 to your computer and use it in GitHub Desktop.
Create Multi-Part Zip Archives
#!/bin/bash
# --- Configuration ---
# Define the maximum size for each zip file in bytes (4 GB)
# 1 GB = 1024 * 1024 * 1024 bytes
MAX_SIZE_BYTES=$((4 * 1024 * 1024 * 1024))
# Define the prefix for the zip file names (e.g., archive_001.zip, archive_002.zip)
ZIP_PREFIX="archive"
# --- Pre-checks ---
# Check if the 'zip' command is available
if ! command -v zip &> /dev/null
then
echo "Error: 'zip' command not found."
echo "Please install it using your package manager (e.g., sudo apt install zip on Debian/Ubuntu, sudo yum install zip on CentOS/RHEL)."
exit 1
fi
# --- Initialization ---
zip_count=1 # Counter for the zip file number
current_zip_size=0 # Accumulator for the size of files in the current zip
declare -a files_to_zip # Array to hold the list of files for the current zip archive
echo "Starting creation of zip archives (max ~4GB per archive)..."
echo "Output zip files will be named '${ZIP_PREFIX}_XXX.zip' and saved in the current directory."
echo "Processing files..."
# --- File Processing Loop ---
# Find all regular files in the current directory and its subdirectories.
# -type f: Ensures only files are processed, not directories.
# -print0: Prints file paths separated by a null character. This is crucial
# for safely handling filenames that contain spaces, newlines, or
# other special characters.
#
# The while loop reads each null-terminated filename.
# IFS= read -r -d $'\0' filepath:
# - IFS=: Prevents leading/trailing whitespace trimming.
# - -r: Prevents backslash escapes from being interpreted.
# - -d $'\0': Specifies the null character as the delimiter.
# - filepath: The variable that will hold the current file's path.
find . -type f -print0 | while IFS= read -r -d $'\0' filepath; do
# Skip the script itself to prevent it from zipping itself
if [[ "$(basename "$filepath")" == "$(basename "$0")" ]]; then
continue
fi
# Skip any already created zip files to avoid re-zipping them
if [[ "$filepath" == "./${ZIP_PREFIX}"*.zip ]]; then
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment