Search files inside an archive (zip/jar/aar) using command line

Many times we need to find some file inside a compressed file. In my case this is when I want to find which jar file consists of a particular class. If you are in linux this can be done very easily. All you have to do is write yourself a small script to do the job for you.

Checkout the following in findjar.sh file

find . -name ‘*.jar’ -exec grep $1 {} \; -print

after saving the file we can find any class inside a jar file from the current directory.

saminda@saminda-laptop:~/.m2/repository$ findjar org.apache.axis2.engine.AxisEngine
Binary file ./org/wso2/carbon/org.wso2.carbon.axis2/1.4/org.wso2.carbon.axis2-1.4.jar matches
./org/wso2/carbon/org.wso2.carbon.axis2/1.4/org.wso2.carbon.axis2-1.4.jar
Binary file ./org/wso2/carbon/org.wso2.carbon.axis2/1.4.1-RC1/org.wso2.carbon.axis2-1.4.1-RC1.jar matches
./org/wso2/carbon/org.wso2.carbon.axis2/1.4.1-RC1/org.wso2.carbon.axis2-1.4.1-RC1.jar
Binary file ./org/wso2/carbon/org.wso2.carbon.axis2/1.4.1/org.wso2.carbon.axis2-1.4.1.jar matches
./org/wso2/carbon/org.wso2.carbon.axis2/1.4.1/org.wso2.carbon.axis2-1.4.1.jar
Binary file ./org/wso2/carbon/org.wso2.carbon.axis2/1.401/org.wso2.carbon.axis2-1.401.jar matches
./org/wso2/carbon/org.wso2.carbon.axis2/1.401/org.wso2.carbon.axis2-1.401.jar
...
...

Ofcourse the above script is only for searching jar files. If you want to search for zip files all you have to do is the change the value of the name parameter specified for the find command (i.e. find -name ‘*.zip’ …….). Simple.

Take note that due to the fact that we are using the find command it will traverse through the whole directory and output the results.

Tags: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: