Friday, August 21, 2009

How to search the contents of a list of jar/tar files

If you have a directory, for example /home/mhewedy/libs/jars/, this directory contains many jar/tar files, and you want to search the contents of these jar/tar files .

use this command :

cd /home/mhewedy/libs/jars/
for jar in *.jar; do echo "--- $jar ---" ; jar tf $jar | grep NullPointerException ; done;

Note, you may also use this command to search a list of tars for its contents:
for tar in *.tar; do echo "--- $tar ---" ; tar tf $tar | grep some_file_name ; done;

4 comments:

Ahmad Amr said...

Does this decompress and search the archived file on the fly?

Mohammed Hewedy said...

yep

Ahmad Amr said...

Amazing! Thanks! Any idea about how we can do the same with a .tar.gz or .tar.bz2 (compressed) file?

mohammed hewedy said...

I just tried to print the contents of a .tgz file with the same command and it went well. i believe it should work too with .bz2 as it or with small modifications.