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
$
zip info
.zip info -t "*zip"
will run on all zip files in the$PWD
. This give infer for each zip archive however if you want a combined total you need to script is so the count for each can be added up.