Skip to content

Instantly share code, notes, and snippets.

@mike1703
Created October 31, 2017 07:04
Show Gist options
  • Save mike1703/47ba2b462a7d68ecff7c5345c7817cfa to your computer and use it in GitHub Desktop.
Save mike1703/47ba2b462a7d68ecff7c5345c7817cfa to your computer and use it in GitHub Desktop.
finds all sqlite databases in the current folder (and subfolders) and executes sqlite3 $FILE "VACUUM;" on all databases
#!/bin/bash
find -type f \( -name "*.db" -or -name "*.sqlite" \) -print0 | while read -d $'\0' FILE
do
TYPE=`file "$FILE" | grep -i sqlite`
[ -z "$TYPE" ] && continue # this is not an sqlite db
PRESIZE=`ls -sh "$FILE" | cut -f 1 -d " "`
PRESIZEFULL=`ls -s "$FILE" | cut -f 1 -d " "`
sqlite3 "$FILE" "VACUUM;"
POSTSIZE=`ls -sh "$FILE" | cut -f 1 -d " "`
POSTSIZEFULL=`ls -s "$FILE" | cut -f 1 -d " "`
PERCENTAGE=`echo "$POSTSIZEFULL*100/$PRESIZEFULL" | bc`
echo -e "$PERCENTAGE%:\t$PRESIZE\t→ $POSTSIZE\t: $FILE"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment