Skip to content

Instantly share code, notes, and snippets.

@sugarmo
Created January 17, 2014 09:29
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugarmo/8470598 to your computer and use it in GitHub Desktop.
Save sugarmo/8470598 to your computer and use it in GitHub Desktop.
Find unused images
#!/bin/bash
# Escape code
esc=`echo -en "\033"`
# Set colors
cc_red="${esc}[0;31m"
cc_green="${esc}[0;32m"
cc_yellow="${esc}[0;33m"
cc_blue="${esc}[0;34m"
cc_normal=`echo -en "${esc}[m\017"`
echo "Creating files index..."
files=(`find . -type f \( ! -path './backup/*' ! -path './External/*' -and \( -name '*.xib' -or -name '*.[mh]' -or -name '*.storyboard' -or -name '*.mm' -or -name '*.html' -or -name '*.css' -or -name '*.js' -or -name '*.plist' \) \) -exec echo ''{}'' \;`)
echo "Creating images index..."
images=(`find . -type f \( ! -path './backup/*' ! -path './External/*' -and \( -name '*.png' -or -name '*.jpg' -or -name '*.jpeg' \) \) -exec echo ''{}'' \;`)
image_count=${#images[@]}
i=0
unused_count=0
echo 'Unused file list:' > ./find-unused_images.log
for image in ${images[*]}
do
let "i += 1"
name=`basename -s .png $image`
name=`basename -s .jpg $name`
name=`basename -s .jpeg $name`
name=`basename -s @2x $name`
if ! grep -qhs "\b$name\b" ${files[*]}; then
let "unused_count += 1"
echo "($i / $image_count) $image ${cc_red}not referenced${cc_normal}"
echo $image >> ./find-unused_images.log
else
echo "($i / $image_count) $image ${cc_green}checked${cc_normal}."
fi
done
echo "$unused_count unused images found. See find-unused_images.log for found list."
echo ''
@caleb15
Copy link

caleb15 commented Feb 6, 2020

the script hung for me when grep'ing so I simplified it a bit:

#!/bin/bash

# Escape code
esc=`echo -en "\033"`

# Set colors
cc_red="${esc}[0;31m"
cc_green="${esc}[0;32m"
cc_yellow="${esc}[0;33m"
cc_blue="${esc}[0;34m"
cc_normal=`echo -en "${esc}[m\017"`

files=(`find . -type f  \( ! -path './backup/*' ! -path './External/*' -and \( -name '*.xib' -or -name '*.[mh]' -or -name '*.storyboard' -or -name '*.mm' -or -name '*.html' -or -name '*.css' -or -name '*.js' -or -name '*.plist' \) \) -exec echo ''{}'' \;`)

images=(`find . -type f \( ! -path './backup/*' ! -path './External/*' -and \( -name '*.png' -or -name '*.jpg' -or -name '*.jpeg' \) \) -exec echo ''{}'' \;`)
image_count=${#images[@]}

i=0
unused_count=0

echo 'Unused file list:' > ./find-unused_images.log

for image in ${images[*]} 
do
	let "i += 1"

	name=`basename -s .png $image`
	name=`basename -s .jpg $name`
	name=`basename -s .jpeg $name`
	name=`basename -s @2x $name`

        echo searching for $name
        grep -Rl $name
        echo
        echo
done

Given the output you can do a regex search for \n.*\n to find unused images (unused images will just have "searching for name" outputted with empty space before and after)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment