Find unused / unreferenced files in a large java-project folder

I am working on  a large Java software project consisting of .java files, Freemarker HTML templates (.ftl, .inc) and javascript files (.js).

Over the years there are files around which are not referenced anywhere anymore. Mainly because developers have forgotten to delete them. For example temporary test files – it happens to all of us 🙂

What I want to do is an easy way to „find all unreferenced files“. Unreferenced means that the filename is not mentioned or found inside any other file (.java, .ftl or .inc).

We are using Eclipse and I wished there was a function there to accomplish this , but I have not found one

Shell to the rescue: the following one-liner command can be used to do this.

 

Find .ftl template files which are NOT referenced in .java files

cd repo
find . -name "*.ftl" -printf "%f\n" |while read fname; do  grep --include \*.java -rl "$fname" . > /dev/null || echo "${fname} not referenced" ;  done;

or

Find all .ftl template files which are NOT referenced in .java, .ftl, .inc files

find . -name "*.ftl" -printf "%f\n" |while read fname; do  grep --include \*.java --include \*.ftl --include \*.inc -rl "$fname" . > /dev/null || echo "${fname} not referenced" ;  done;

Note: on MacOSX you can use gfind instead of find in case -printf is not working (brew install findutils)

Example result:

find . -name "*.ftl" -printf "%f\n" |while read fname; do  grep --include \*.java --include \*.ftl --include \*.inc -rl "$fname" . > /dev/null || echo "${fname} not referenced" ;  done; 
testfile.ftl not referenced
getInfoMessage_temp.ftl not referenced

 

Dieser Beitrag wurde unter Software-Development abgelegt und mit , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.