Grep inside multiple directories and archived files

I have many 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 zipped files in a directory.

To deploy it, I have many such directories. How to find out the total number of json files in all zipped files in all directories?

+4
source share
2 answers

With the find you can easily iterate over all directories and map them to a named template, such as *.zip .

When you go to the list returned by find (the for loop will be good here), you will need to specify the files in each archive (you will not need to extract the files, which is good) and while you list the files, you can do a simple grep to find the template .json and the channel that displays on wc -l , which will give you a "line count" - in this case it will represent the number of .json .

During each iteration, you must take this account and add it to the "total" account, which you can then withdraw later.

An extended example of this:

 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 example assumes that you are using zip to archive your files. If you use something like tar , you will need to use its file list options ( tar -t ).

+2
source

zgrep works like grep, but it processes zip files. On linux there is a shell, on bsd and osx its binary.

+1
source

Source: https://habr.com/ru/post/1478863/


All Articles