Create a gist now

Instantly share code, notes, and snippets.

Script that looks at all images in a given location and checks if there are any references to each image. Useful for checking if images are being used by CSS for instance.
#!/bin/bash
DIR=.
if [ -n "$1" ]
then
DIR=$1
fi
# Find image files in.
FILES=`find $DIR -type f | grep ".*\.\(jpg\|gif\|png\|jpeg\)"`
# Default searcher is grep. If Silver Searcher is installed, use that.
SEARCHER='grep -r -l '
if command -v ag
then
# Sweet! Let's use Silver Searcher.
SEARCHER='ag -l '
fi
# Loop over image files.
for f in $FILES
do
if [[ -f $f ]]
then
name=$(basename $f)
found=$($SEARCHER $name $DIR)
if [[ -z $found ]]
then
echo $f
fi
fi
done
@stanosmith

This works well, except when I'm using Silver Searcher (ag version 0.23.0, installed with homebrew) it outputs the location of ag (/usr/local/bin/ag). So if I pipe to xargs rm, ag is removed and I have to relink. I've played with the script a bit to see if I could fix it, but I'm not familiar with shell scripting. Any thoughts?

@selahattinunlu

Thanks :)

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