How to remove unused resource files from a project (script)

This post provides a script to list out the unused .png resource files in your project, after which you can manually remove the files from your project. This script can be fine-tuned to do more, as is explained in the post.

The project that I am currently working on is my iPhone app called AgreeOn. It has been through multiple versoins and by now a lot of the resource files are out of date and are not used anymore. Moreover, the app needs to be redesigned for iOS7 so I will be doing away with most of the custom UI design anyway.

One issue I am facing is that I have so many resource files (.png) that are unused and have been accummulated during the initial development of the project. Now, I want to clean it all up and make it less bloated and reduce the entropy of my code.

For this, I want to deleted any .png files that are unused in the project. I started out by deleting those that I knew were unused and deleting them, however, as you can tell, this is not the most efficient process, nor is it the most accurate. So I decided to hack together a script that will help me out with this.

This script lists out all the .png files that are currently unused in the project:

for file in `find . | grep .png`; do name="${file:2:${#file}-6}"; used=`find . | xargs grep $name 2>/dev/null | wc -l`; if [ $used -eq 0 ]; then echo $file; fi; done;

By looking at this script a little closer, you will be able to tell how it accomplishes this, here is the pseudocode for it::

1. for all .png files in the currrent directory:

    a. get the filename as "name" (without the extension)

    b. search recursively in the current directory for all files that contain the name, redirect stderr to /dev/null

    c. get the number of lines in (b) and save it as "used"

    d. if there was nothing found in (b), i.e. if used was 0, then print out the filename


This script can be made more efficient by caching results in variables (like for "find ." for example), however, I will not include that here as that strays from the purpose of this post. Another helpful script may be to list out all the .png files and where they are used in the project. Here is the script:

for file in `find . | grep .png`; do name="${file:2:${#file}-6}"; echo $name; find . | xargs grep $name 2>/dev/null; echo ""; done;

I will leave it to you to figure out how it accomplishes this.

Show comments 0