Thursday, May 06, 2010

Finding all JAVA imports in a Specified Directory (& sub-directories)

Here is a little (Li | U)nix command line script that looks through a given directory and extracts all of the JAVA import statements, sorts them and then prints them to STDOUT.

find ./someJavaDir/ -type f -name '*.java' \
-exec grep '^import .*$' '{}' \; | \
sort | uniq | sort | awk '/import/ {print $2 }'

Basically, replace ./someJAVADir/ with the actual directory that you would like to search for JAVA imports.