Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

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

View clean_unused_images.sh
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#!/bin/sh
 
#vars
if [ "$#" == 0 ]; then
ROOT_FOLDER="."
else
ROOT_FOLDER=$1
fi
#we 'assume' only images here and in subfolders
IMAGES_FOLDER="./Resources/images/"
LOG_USAGE=1
 
cd $ROOT_FOLDER
echo "Working Dir: " . `pwd`
echo "Images Folder: $IMAGES_FOLDER"
 
# Update our list of images
echo "---"
echo "Collect list of images..."
`find $IMAGES_FOLDER -type f -exec basename {} \; | grep @2x | sort | cut -f 1 -d @ > CUI_sortedRetinas`
`find $IMAGES_FOLDER -type f -exec basename {} \; | grep --invert-match @2x | sort | cut -f 1 -d . > CUI_sortedNonRetinas`
 
#diff the arrays and check for all versions
echo "---"
echo "diff the array and check for all versions..."
diff_missing_retinas=`comm -13 CUI_sortedRetinas CUI_sortedNonRetinas`
if [ "$diff_missing_retinas" != "" ]; then
echo "$diff_missing_retinas" > CUI_missingRetinas
fi
diff_missing_nonretinas=`comm -23 CUI_sortedRetinas CUI_sortedNonRetinas`
if [ "$diff_missing_nonretinas" != "" ]; then
echo "$diff_missing_nonretinas" > CUI_missingNonRetinas
fi
echo "done"
 
#Collect unused files (based on retinas for now)
echo "---"
echo "Collect unused files (based on retinas)..."
images=`cat CUI_sortedRetinas`
nonUsedImages=()
for imageName in $images; do
used=`grep -rl -m 1 "$imageName" . --exclude-dir=*.xcodeproj, --exclude-dir=.svn --exclude-dir=.git --exclude=CUI_*`
if [ "$used" = "" ]; then
nonUsedImages+=($imageName)
fi
 
if [ "$LOG_USAGE" = 1 ]; then
if [ "$used" = "" ]; then
echo "$imageName seems to be unused"
nonUsedImages+=($imageName)
else
echo "$imageName still used: First use in:" . $used
fi
fi
done
echo "done"
 
#delete unused files (based on non retinas)
echo "---"
echo "Write RM commands to disk, so this file could be passed to the shell directly (if approved)..."
#`rm -f CUI_rm`
for imageName in "${nonUsedImages[@]}"; do
echo "rm $IMAGES_FOLDER/$imageName*" >> CUI_rm
done
 
#rm temp files
echo "---"
echo "rm temp files..."
`rm CUI_sortedRetinas`
`rm CUI_sortedNonRetinas`
echo "done"
 
echo "all done!"
Owner

2015 and I updated this gist again ;) to work recursively

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.