Skip to content

Instantly share code, notes, and snippets.

@jag1989
Last active June 19, 2018 13:39
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 jag1989/b17752899de3bbb91ac227eb962e36d5 to your computer and use it in GitHub Desktop.
Save jag1989/b17752899de3bbb91ac227eb962e36d5 to your computer and use it in GitHub Desktop.
Extract asset paths and delete unused
cd ~/www/mduk/htdocs && find assets -type f > ./asset-paths-source-test.txt
count=$(wc -l < ~/www/mduk/htdocs/asset-paths-source-test.txt)
echo "Created ~/www/mduk/htdocs/asset-paths-source-test.txt"
echo "Total Asset Paths: $count"
grep "\/assets\/[0-9]{4}\/[0-9]{4}\/(.*?)(\.(doc|pdf|jpg|jpeg|png|gif|mp3|mov|wmv|rtf|mp4|com|swf)|\'|\"|\s|\<\/)" -E -i -o ~/Downloads/wp-content/mysql.sql | perl -ne '$H{$_}++ or print' > asset-paths-uniq.txt
sed -i.bak -e 's/^\///' -e 's/\\\"$//' asset-paths-uniq.txt
grep -v -f asset-paths-uniq.txt ~/www/mduk/htdocs/asset-paths-source.txt > assets-unused.txt
while read file; do
path="/Users/jonathangarrett/www/mduk/htdocs/"
rm $path$file
done < assets-unused.txt
cd ~/www/mduk/htdocs/assets && find . -empty -type d -delete
countassetfolders=$(cd ~/www/mduk/htdocs/assets && ls -lR | grep "^d" | wc -l)
fullassetlist=$(wc -l < ~/www/mduk/htdocs/asset-paths-source-test.txt)
uniqueassets=$(wc -l < asset-paths-uniq.txt)
toberemoved=$(wc -l < assets-unused.txt)
echo "Source Total Asset Paths: $fullassetlist"
echo "Source Total Asset Folders $countassetfolders"
echo "Unique Asset Paths in SQL: $uniqueassets"
echo "Count to be removed: $toberemoved"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment