Skip to content

Instantly share code, notes, and snippets.

@artsyca
Forked from rafaelp/find_unused_images.rake
Last active December 18, 2015 09:09
Show Gist options
  • Save artsyca/5759418 to your computer and use it in GitHub Desktop.
Save artsyca/5759418 to your computer and use it in GitHub Desktop.
Updated to use newer ack. Only outputs the filenames. You can use: $ rake find_unused_images > unused_images.txt $ xargs rm < ./unused_images.txt to delete them all! Also, supports multiple directories.. and filenames are shellescaped.
# It requires ACK - http://betterthangrep.com/
# Modified from https://gist.github.com/rafaelp/4467738
# Please add the following filetypes to your .ackrc
# --type-set=haml=.haml
# --type-set=sass=.sass
# --type-set=scss=.scss
# --type-set=erb=.erb
# --type-set=coffee=.coffee
# Based on: https://github.com/docwhat/homedir-examples/pull/3
task :find_unused_images do
# Look in two folders by default.
directories = [
'app/assets/images/**/*',
'public/images/**/*'
]
#puts "=======================\nDelete unused files below:"
directories.each do |path|
images = Dir.glob(path)
images.each do |image|
unless File.directory?(image)
result = `ack -1 --ignore-directory=tmp --ignore-directory=log --ruby --html --css --js --sass --scss --coffee --erb --haml #{File.basename(image).shellescape}`
if result.empty?
print "./" + image.shellescape + "\n"
end
end #unless File.directory?(image)
end #images.each
end #directories.each
end #task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment