Skip to content

Instantly share code, notes, and snippets.

@henriquemoody
Created January 6, 2012 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henriquemoody/1570710 to your computer and use it in GitHub Desktop.
Save henriquemoody/1570710 to your computer and use it in GitHub Desktop.
Find all occurrences of the files of DIR into other files and delete then
#!/bin/bash
HELP="Usage: $(basename ${0}) DIR
or: $(basename ${0}) --help
Find all occurrences of the files of DIR into other files and delete then if
there is no occurrence.
--help display this help and exit
Exemplos:
$(basename ${0}) web/images
$(basename ${0}) web/css
Report bugs to <fix@it.yourself>.";
if [ ! "${1}" ]
then
echo "${HELP}" 1>&2;
exit 1;
elif [ "${1}" == '--help' ]
then
echo "${HELP}";
exit 0;
fi;
find "${1}" -name '*.*' | sort | while read filename;
do
basename=$(basename "${filename}");
pattern=$(echo "${basename}" | sed 's/\./\\./g');
occurrences=$(egrep -R "\b${pattern}\b" --include=*.* . | wc -l);
message="${basename} has ${occurrences} occurrences";
if [ ${occurrences} -eq 0 ]
then
message="${message}: $(rm -fv ${filename})";
fi;
echo "${message}";
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment