MacOS – List number of files in multiple zip files

compressionmacosterminalzip

How can I get the total number of files in multiple zip files at the same time? I have around 1000 zipfiles in one folder and would like to know the total amount of files inside them all combined. How can I do this? I've tried

zipinfo -t file.zip 

But that only gives me the number from that archive, I've also tried

zipinfo -t *zip/?.zip

and gets

caution: filename not matched:  Jo Coo-Day-Sun.zip
caution: filename not matched:  Micke Mouse-Stare-Well-Pt1.zip
caution: filename not matched:  Micke Mouse Cooley-Stare-Well-Pt2.zip

Any suggestions?

Best Answer

There is certainly more then one way to do this and what I'm presenting is not necessarily the best way, however it is a way that works. Create a bash script using the following code.

#!/bin/bash
c=0
for f in *.zip; do
    x="$(zipinfo -t "$f" | awk '{print $1}')"
    c=$(( $c + $x ))
done
echo "The total file count is:" $c

Then you'd cd to the directory containing the zip archive files and execute the bash script by its name if it's in the $PATH or its pathname if it's not in the $PATH.

Say you save it as getfilecount in your $HOME directory, which normally is not in your $PATH you'd cd to the directory containing the zip archive files and then use:

~/getfilecount

To make the bash script create an empty text file, e.g. touch getfilecount and then open the file, e.g. open getfilecount add the code above via copy and paste and save it. Now make the file executable, e.g chmod +x getfilecount and now you can use it as is or place it in a directory that's in the $PATH, then all you'd need to type once changing to the directory containing the zip archive files is: getfilecount

Below is sample output to show the difference between using single commands method and a bash script.

Issuing commands, one at a time:

$ cd zipfiles
$ ls
codetest.zip    destination.zip helloworld.zip  source.zip
$ zipinfo -t "*.zip"
1 file, 820 bytes uncompressed, 437 bytes compressed:  46.7%

1 file, 0 bytes uncompressed, 0 bytes compressed:  0.0%

6 files, 12385 bytes uncompressed, 895 bytes compressed:  92.8%

101 files, 0 bytes uncompressed, 0 bytes compressed:  0.0%

4 archives were successfully processed.
$

Using getfilecount (when the bash script is in the $PATH):

$ cd zipfiles
$ getfilecount

The total file count is: 109
$