Skip to content

Instantly share code, notes, and snippets.

@carlovsk
Last active July 1, 2025 13:52
Show Gist options
  • Save carlovsk/4b6d0667dbd939ba2d58c8a7e00b49d8 to your computer and use it in GitHub Desktop.
Save carlovsk/4b6d0667dbd939ba2d58c8a7e00b49d8 to your computer and use it in GitHub Desktop.
zip_env_keys.sh
#!/usr/bin/env bash
set -euo pipefail
# -----------------------
# Configuration
# -----------------------
SRC_DIR="${HOME}/www"
DEST_DIR="${HOME}/Desktop/envs"
ZIP_PATH="${HOME}/Desktop/envs.zip"
# -----------------------
# Find and copy .env files
# -----------------------
find "$SRC_DIR" -type f -name '.env' | while IFS= read -r src_file; do
# compute path relative to SRC_DIR
rel_path="${src_file#$SRC_DIR/}"
dest_dir="$(dirname "$DEST_DIR/$rel_path")"
# make sure the target directory exists
mkdir -p "$dest_dir"
# copy only if source is newer than dest, or dest doesn't exist
if [[ ! -e "$dest_dir/.env" ]] || [[ "$src_file" -nt "$dest_dir/.env" ]]; then
cp "$src_file" "$dest_dir/"
echo "copied: $rel_path"
fi
done
# -----------------------
# Zip the entire envs folder
# -----------------------
# -q : quiet
# -r : recursive
# -FS: only include changed files (makes zip faster/smaller)
zip -q -r -FS "$ZIP_PATH" "$DEST_DIR"
echo "zipped to: $ZIP_PATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment