Can a zsh script to batch zip files work recursivly? (VikingOSX script in post)

Last month VikingOSX graciously coded a script to batch process particular sets of files into archive zips that take on the name of the files. From this post. It rocks. I've zipped about 50,000 files since then.


I have recently learned a new vocabulary word: recursive. Yes, I'm new to the command line.


Could the script below be changed so that it works recursively on all sets of designated files (jpg, png, eps, svg, in this case) in the directory AND subdirectories of that directory? If so, would it work even if some directories had no files? And would it ignore (not archive) any other file type than the ones designated in the script ((jpg, png, eps, svg)?


#!/bin/zsh

: <<"COMMENT"
For a given folder containing filenames identified by product code and suffix,
aggregate the four image files associated with it into a zip container.

For example: Dzn-4-n-m-no.zip contains the four images: Dzn-4-n-m-no.{eps,jpg,png,svg}

Tested: macOS 14.1.2 (Zsh 5.9), macOS 10.14.6 (Zsh 5.3)
Revision: 2
Author: VikingOSX, 2023-12-03, Apple Support Communities, No warranties implied.

Usage: lab.zsh folder
COMMENT

# absolute path of folder passed as only argument to the script
ZIPDIR="${1:a:s/\~\//}"

# image types to process
EXTPAT="eps|jpg|png|svg"
last_ext=
last_fname=

setopt nocaseglob
for f in "${ZIPDIR}"/*.(${~EXTPAT})(.Non);
do
    # Two captures: product name and two or three character suffix
    [[ "${f:t}" =~ "(^.*\-)([[:alpha:]]{2,3})\..*$" ]] || continue
    [[ $last_fname != "${match[1]}"  || $last_ext != "${match[2]}" ]] || continue
    last_fname="${match[1]}"
    last_ext="${match[2]}"
    # capture unique suffixes to their respective zip file excluding dot files
    zip -qj -6 "${ZIPDIR}/${(j::)match[@]}".zip "${ZIPDIR}/${(j::)match[@]}".{eps,jpg,png,svg} -x ".*"
    # rm -f "${ZIPDIR}/${(j::)match[@]}".{eps,jpg,png,svg}
done
zipcnt=( "${ZIPDIR}"/*.zip )
echo "Zip archives created: $#zipcnt"
unset zipcnt
exit 0



Posted on Jan 14, 2024 1:10 AM

Reply

Similar questions

41 replies

Feb 16, 2024 4:24 PM in response to swapot

VikingOSX:


I've been using your bash scripts for weeks and they are awesome!


I wonder about another script like this, that I think you had something similar in another thread/post, but I cannot find.


Do you have script that can compress/zip all of the files in a folder/directory that share the same suffix?


In my case, I want to run a zip on a folder of files and archive the files together into zips that have the same three letter suffix (before the filename extension). For example:


filename-name-1-ber.eps

filename-name-1-ber.jpg

filename-name-1-ber.png

filename-name-1-ber.svg

filename-name-1-stb.eps

filename-name-1-stb.jpg

filename-name-1-stb.png

filename-name-1-stb.svg

filename-name-2-ber.eps

filename-name-2-ber.jpg

filename-name-2-ber.png

filename-name-2-ber.svg


It would zip the 8 files that end in "ber" into a zip and the 4 files that end in "stb" into a zip. Not sure how it would name the zip files - perhaps name them by taking the fist (prefix) word of the files and add the three letter suffix with a hyphen in between?


Just thought I had seen something like this that you or someone else did on this forum.

Feb 18, 2024 5:56 PM in response to VikingOSX

Yes, both captures are alphanumeric.


Here are all of the one-character alphanumeric characters that appear in the place of the first (x) in filename-name-(x)-(xxx).ext:


(l) (o) (v) (e) (a) (m) (r) (h) (d)


*Note that the character (d) is not a single character (it is not preceded by a hyphen) - it is the last character in the word (word), as in the real file name example below:


LOVE-Block-e-ber.eps

LOVE-Block-e-ber.jpg

LOVE-Block-e-ber.png

LOVE-Block-e-ber.svg

LOVE-Block-e-blr.eps

LOVE-Block-e-blr.jpg

LOVE-Block-e-blr.png

LOVE-Block-e-blr.svg

...

LOVE-Block-l-ber.eps

LOVE-Block-l-ber.jpg

LOVE-Block-l-ber.png

LOVE-Block-l-ber.svg

LOVE-Block-l-blr.eps

LOVE-Block-l-blr.jpg

LOVE-Block-l-blr.png

LOVE-Block-l-blr.svg

...

LOVE-Block-o-ber.eps

LOVE-Block-o-ber.jpg

LOVE-Block-o-ber.png

LOVE-Block-o-ber.svg

LOVE-Block-o-blr.eps

LOVE-Block-o-blr.jpg

LOVE-Block-o-blr.png

LOVE-Block-o-blr.svg

...

LOVE-Block-v-ber.eps

LOVE-Block-v-ber.jpg

LOVE-Block-v-ber.png

LOVE-Block-v-ber.svg

LOVE-Block-v-blr.eps

LOVE-Block-v-blr.jpg

LOVE-Block-v-blr.png

LOVE-Block-v-blr.svg

...

LOVE-Block-word-ber.eps

LOVE-Block-word-ber.jpg

LOVE-Block-word-ber.png

LOVE-Block-word-ber.svg

LOVE-Block-word-blr.eps

LOVE-Block-word-blr.jpg

LOVE-Block-word-blr.png

LOVE-Block-word-blr.svg


Also to note, not all of those (x) fifth from last characters are in every directory. If it matters, here is a breakdown of directories and the characters they have:


Directory 1: (l) (o) (v) (e) (d)

Directory 2: (a) (m) (o) (r) (d)

Directory 3: (a) (m) (o) (r) (e) (d)

Directory 4: (a) (l) (o) (h) (a) (d)


What I need to end up with from the real filename example further above is:


LOVE-Block-ber.zip (containing the 20 files ending in "ber")

LOVE-Block-blr.zip (containing the 20 files ending in "blr")


The current script zips the 20 files and names the zip "ber.zip", "blr.zip", etc., and then I just have the key in or paste in to append the "LOVE-Block" or other prefixes. If there was something added in front of the "ber", it would be easier/faster to do a find/replace with Mac Finder (but I can do the pasting in if this is complicated work!).


Thanks!

Feb 19, 2024 5:29 PM in response to VikingOSX

It works - however, as I mentioned, the (d) character in the -x- place is not a single letter "d" that is preceded and followed by a hyphen. It is the last letter of the word "word", as in the example below:


LOVE-Block-word-ber.eps

LOVE-Block-word-ber.jpg

LOVE-Block-word-ber.png

LOVE-Block-word-ber.svg

LOVE-Block-word-blr.eps

LOVE-Block-word-blr.jpg

LOVE-Block-word-blr.png

LOVE-Block-word-blr.svg


The script is ignoring those files (I assume because it is not a single letter?), whereas I need those in the zip.


If it's an easy fix to have those included, great. If not, just let me know and I'll use the first one you made.


Much thanks.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Can a zsh script to batch zip files work recursivly? (VikingOSX script in post)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.