public
Created

Verify and filter potentially unused files -- in our case, I used it to find missing retina images and all-together unused ones

  • Download Gist
clean_unused_images.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#!/bin/sh
 
#vars
if [ "$#" == 0 ]; then
ROOT_FOLDER="."
else
ROOT_FOLDER=$1
fi
 
IMAGES_FOLDER="$ROOT_FOLDER/Resources/src/Images"
 
# Update our list of images
echo "Collect list of images..."
`ls $IMAGES_FOLDER | grep @2x | sort | cut -f 1 -d @ > _sortedRetinas`
`ls $IMAGES_FOLDER | grep --invert-match @2x | sort | cut -f 1 -d . > _sortedNonRetinas`
images=`cat _sortedNonRetinas`
 
#diff the arrays and check for all versions
echo "diff the array and check for all versions..."
diff_missing_retinas=`comm -13 _sortedRetinas _sortedNonRetinas`
if [ "$diff_missing_retinas" != "" ]; then
echo "$diff_missing_retinas" > clean_unused_images_missingRetinas
fi
diff_missing_nonretinas=`comm -23 _sortedRetinas _sortedNonRetinas`
if [ "$diff_missing_nonretinas" != "" ]; then
echo "$diff_missing_nonretinas" > clean_unused_images_missingNonRetinas
fi
echo "done"
 
#rm temp files
echo "rm temp files..."
`rm _sortedRetinas`
`rm _sortedNonRetinas`
echo "done"
 
#Collect unused files (based on non retinas)
echo "Collect unused files (based on non retinas)..."
nonUsedImages=()
for imageName in $images; do
used=`grep -rl "$imageName" . --exclude-dir=*.xcodeproj`
if [ "$used" = "" ]; then
# echo "$imageName unused"
nonUsedImages+=($imageName)
# else
# echo "$imageName still used"
fi
done
echo "done"
 
#Collect unused files (based on non retinas)
echo "Write RM commands to disk, so this file could be passed to the shell directly (if approved)..."
`rm -f clean_unused_images_rm`
for imageName in "${nonUsedImages[@]}"; do
echo "rm $IMAGES_FOLDER/$imageName*" >> clean_unused_images_rm
done
 
echo "all done!"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.