Skip to content

Instantly share code, notes, and snippets.

@acodeninja
Created April 12, 2019 09:49
Show Gist options
  • Save acodeninja/c49775003e5321922e891a08e81e9ff7 to your computer and use it in GitHub Desktop.
Save acodeninja/c49775003e5321922e891a08e81e9ff7 to your computer and use it in GitHub Desktop.
Clean out temporary image files from a directory of your choosing.
#!/usr/bin/env bash
FILE_AGE=$1
FILE_COUNT=$2
FILE_LOCATION=$3
if [ -z "$FILE_AGE" ] || [ -z "$FILE_COUNT" ] || [ -z "$FILE_LOCATION" ]; then
echo "Usage: $(basename $0) [file age in days] [file count] [directory location]"
exit 1
fi
find $FILE_LOCATION -type f -print -mtime +$FILE_AGE \
| file --mime-type -f - \
| grep 'image/' \
| head -$FILE_COUNT \
| while read -r line ; do
IFS=':'
read -ra ADDR <<< "$line"
for i in "${ADDR[@]}"; do
echo $i
done
IFS=' '
done \
| grep '^\/' \
| while read -r line ; do
rm "$line"
echo $line >> /tmp/file-cleanup.log
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment