Skip to content

@Daij-Djan /clean_unused_images.sh
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Verify and filter potentially unused files -- in our case, I used it to find missing retina images and all-together unused ones
#!/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!"
@Daij-Djan
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.