Skip to content

Instantly share code, notes, and snippets.

@lefou
Created February 8, 2018 09:22
Show Gist options
  • Save lefou/6696ff5f0e1bd9c18e95291eec873128 to your computer and use it in GitHub Desktop.
Save lefou/6696ff5f0e1bd9c18e95291eec873128 to your computer and use it in GitHub Desktop.
Find JAR files and searches their content listings for a given pattern
#!/bin/bash
# Default behavior is scan directory recursively
if [ "x$1" == "x" ]; then
echo "Find JAR files and searches their content listings for a given pattern"
echo
echo "Usage: jarfind [-l] <SEARCHTERM> [[DIR] ...]"
echo
echo "If DIRs is empty, $HOME/.m2/repository is searched."
exit 0
fi
if [ "x$1" = "x-l" ] ; then
ONLY_FILES=1
shift
fi
SEARCH="$1"
shift
if [ "x$1" = "x" ]; then
JARS="$(find $HOME/.m2/repository -iname \*\.jar)"
else
JARS=""
for i in $* ; do
JARS="$JARS $(find $i -iname \*\.jar)"
done
fi
for i in $JARS; do
RESULT="$(unzip -l $i | grep -i $SEARCH)"
if [ "x$RESULT" != "x" ] ; then
echo Jarfile: $i
if [ "x${ONLY_FILES}" != "x1" ] ; then
# we rerun the search command to have colored output fronm grep
unzip -l $i | grep --color -i $SEARCH
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment