Reputation: 5041
I have a lot of zipped files, where each contains json files and txt files in a directory. I want to find the total number of json files in all the zipped files in the directory.
To drill it down, I have a lot of such directories.
How do I find out total count of json files within all the zipped files within all directories?
Upvotes: 4
Views: 3481
Reputation: 326
zgrep works like grep but handles zip files. On linux its a shell wrapper, on bsd and osx its a binary.
Upvotes: 1
Reputation: 38436
Through the find
command, you can easily iterate through each directory you have and match against a named pattern such as *.zip
.
When you go through the list that find
returns (a for
loop would be good here) you will need to list the files in each archive (you won't have to extract the files, which is good) and while you're listing the files you can do a simple grep
to find the .json
pattern and pipe that output to wc -l
which will give you a "line count" - in this case it will represent the number of .json
files.
Throughout the each iteration, you'd take this count and add it to a "total" count which you can then later output.
An expanded sample of this would be:
total=0;
for file in `find . -name '*.zip'`; do
count=`unzip -l $file | grep '.json' | wc -l`;
total=`expr $total + $count`;
done;
echo "Total Json Files: $total";
This sample assumes you're using zip
to archive your files. If you're using something like tar
you'll need to use its file-listing parameters (tar -t
).
Upvotes: 2