How to Batch Compress Files on Mac (Automate with a Bash Script)

If you frequently need to zip large numbers of files on your Mac, you may have found the process to be time-consuming and repetitive. While the Archive Utility built into Mac OS can be used to zip files in batches, it can still be a manual process. However, with a little bit of scripting knowledge, you can automate the process of batch-zipping files using a Bash script.

In this article, we will guide you through the process of creating a Bash script that will allow you to zip files in batches of any number you choose, on your Mac. Whether you are working with hundreds or thousands of files, this script will help you save time and avoid the monotony of zipping files manually. By the end of this tutorial, you will have a fully-functional Bash script that you can use to batch zip files in any quantity you need, with just a few simple commands.

batch zip files in mac

For example, let’s say you have 100 files in a particular folder. You can use this script to compress the files into groups of 10, resulting in 10 compressed files in total. Alternatively, you can compress the files into groups of 5, resulting in 20 compressed files in total, and so on.

#!/bin/bash

# Set the directory path to the folder containing the files to be compressed
DIR_PATH="/path/to/folder"

# Set the name prefix of the output archive files
ARCHIVE_PREFIX="archive"

# Set the maximum number of files per batch
MAX_FILES=20

# Change directory to the specified path
cd "$DIR_PATH"

# Get a list of all files in the directory
files=( * )

# Calculate the number of batches of files
num_batches=$(( (${#files[@]} + $MAX_FILES - 1) / $MAX_FILES ))

# Loop through each batch of files
for (( i=0; i<$num_batches; i++ )); do
    # Set the start and end indices of the batch
    start=$(( $i * $MAX_FILES ))
    end=$(( ($i + 1) * $MAX_FILES - 1 ))
    
    # Check if the end index exceeds the number of files
    if (( $end >= ${#files[@]} )); then
        end=$(( ${#files[@]} - 1 ))
    fi
    
    # Create a compressed archive file for the batch of files
    archive_name="${ARCHIVE_PREFIX}_${i}.tar.gz"
    tar -cvzf "$archive_name" "${files[@]:$start:$MAX_FILES}"
done

Download script.

What This Script Does?

The above script is a bash script that compresses multiple files into batch archives.

It first sets the path to the folder (DIR_PATH="/path/to/folder") containing the files to be compressed, the name prefix of the output archive files (ARCHIVE_PREFIX="archive"), and the maximum number of files per batch (MAX_FILES=20).

It then changes the current directory to the specified path and gets a list of all files in that directory. The script then calculates the number of batches of files and loops through each batch.

For each batch, it sets the start and end indices of the batch, checks if the end index exceeds the number of files, and then creates a compressed archive file for the batch of files using the ‘tar’ command.

The resulting compressed archives are named using the prefix specified and numbered sequentially.

How to Use the Script?

Step 1.

To batch zip files on your Mac, first create a new folder and place all the files you want to compress inside that folder.

For example, you could create a folder called ‘zipme’ on your Desktop and place all the relevant files inside that folder. Once you have done this, you can find the path to the folder by navigating to it in Finder and then right-clicking and selecting ‘Get Info‘.

Alternatively, you can use the pwd command in Terminal to display the current directory and then append the folder name to the end of the path. And in our case, path to zipme/ is: /Users/hongkiat/Desktop/zipme, so we will do the following:

edit:

DIR_PATH="/path/to/folder"

to becoming:

DIR_PATH="/Users/hongkiat/Desktop/zipme"
Step 2.

Copy and paste the code above into a new file on your Mac using a text editor like TextEdit or Atom. Save the file with a .sh extension (e.g. batch_zip.sh) and make it executable by running the following command in Terminal:

chmod +x /path/to/batch_zip.sh
Step 3.

Then, to run the script, open Terminal and navigate to the directory where the script is saved. Type the following command and press Enter:

./batch_zip.sh

Note: If running ./batch_zip.sh doesn’t work, try using sudo ./batch_zip.sh instead. Keep in mind that “sudo” requires authorization by entering the password for the current Mac username.

Once executed, all the files inside the specified folder will be compressed into batches of up to 20 files each, and an archive file will be created for each batch with the specified prefix and an incremental number.

WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail