Last active
June 13, 2026 05:57
-
-
Save maddisondesigns/0f7424cc8faa8b4b750fc4a03c239cde to your computer and use it in GitHub Desktop.
macOS Command Line Zip script using 7z
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 | |
| # Zip Script using 7z | |
| # Requires: 7z (7-Zip) if not installed. Download it from 7-zip.org | |
| # Exit on any error | |
| set -euo pipefail | |
| # Specify the folder location of the 7zip executable file | |
| ZIPLOCATION=$HOME"/Downloads/7Zip" | |
| # Check if 7z is installed | |
| if ! command -v $ZIPLOCATION/7zz &> /dev/null; then | |
| echo "❌ 7z (7-Zip) is not installed. Download it from 7-zip.org" | |
| exit 1 | |
| fi | |
| # Check for correct usage | |
| if [ $# -lt 2 ]; then | |
| echo "❌ No paramaters specified" | |
| echo "Usage: $0 <output_name.7z> <source_file_or_folder> [<optional_source_file_or_folder>]" | |
| exit 1 | |
| fi | |
| COUNTER=0 | |
| OUTPUT="$1" | |
| FILESNOTEXIST=0 | |
| # Check if source exists | |
| for SINGLEFILE in "$@" | |
| do | |
| COUNTER=$((COUNTER + 1)) | |
| if [ $COUNTER -gt 1 ]; then | |
| # Check if source exists | |
| if [ ! -e "$SINGLEFILE" ]; then | |
| echo "Source '$SINGLEFILE' does not exist." | |
| FILESNOTEXIST=1 | |
| fi | |
| fi | |
| done | |
| # Exit if one or more source files do not exist | |
| if [ 1 -eq "$FILESNOTEXIST" ]; then | |
| echo "❌ One of more source files do not exist." | |
| exit 1 | |
| fi | |
| # Prevent overwriting existing archive | |
| if [ -e "$OUTPUT" ]; then | |
| echo "⚠️ Output file '$OUTPUT' already exists. Append to existing file? (Y/N): " | |
| read -r CONFIRM | |
| if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then | |
| echo "❌ Aborted to prevent overwrite." | |
| exit 1 | |
| fi | |
| fi | |
| # Run the 7z command with max compression, AES-256 | |
| $ZIPLOCATION/7zz a -x\!".DS_Store" -r0 -t7z -mx=9 -m0=lzma2 "$@" | |
| echo "✅ Archive created: $OUTPUT" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment