Skip to content

Instantly share code, notes, and snippets.

@naxoc
Created October 28, 2013 20:14
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save naxoc/7203765 to your computer and use it in GitHub Desktop.
Save naxoc/7203765 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

Thanks :)

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