Created
October 28, 2013 20:14
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?