How to get rid from unused entries in ~/.thumbnails

Todd Zullinger tmz at pobox.com
Wed Oct 28 15:39:01 UTC 2009


Tim wrote:
> Hmm, I hadn't thought about using that to clear away personal files.
> I've occasionally wiped out that directory, but hadn't looked at it
> recently.  I see mine's wasting lots of drive space:
>
>  ~]$ du -h .thumbnails/
> 560M	.thumbnails/large
> 361M	.thumbnails/normal
>
> Sounds extreme, but then I do a lot of photography work.

When disk costs fractions of pennies, is that amount of space even a
concern?  I've had my thumbnails dir grow over several GB before,
after moving my photos to new locations.  sing the thumbnails are
stored based on the md5 of the path to the image, I ended up
duplicating a lot of thumbnails.

I believe recent nautilus will try to clean up old/invalid thumbnails,
but I don't use nautilus so I've not tested that.

It is pretty simple to whip up a script to delete thumbnails for files
that don't exist anymore.  Here's a little bit of python that might be
a start for anyone interested.  This only checks the normal size
thumbnails, but would be easy to tweak so it checks large ones as well
(I didn't have any large thumbnails when I wrote it).

#!/usr/bin/python -tt

import os
import sys
import Image
import optparse
from urllib import unquote
from urlparse import urlsplit

default = {
    'dryrun': False,
    'skip_netpaths': False,
    'thumbdir': os.path.expanduser('~/.thumbnails'),
    'verbose': 0,
}

usage = '%prog [options]'
parser = optparse.OptionParser(usage=usage)
parser.add_option('-n', '--dry-run', dest='dryrun',
                  action='store_true', default=default['dryrun'],
                  help='Perform a trial run with no changes made [%default]')
parser.add_option('-N', '--skip-netpaths', dest='skip_netpaths',
                  action='store_true', default=default['skip_netpaths'],
                  help='Skip paths on common network file systems')
parser.add_option('-t', '--thumbdir', dest='thumbdir',
                  default=default['thumbdir'], metavar='dir',
                  help='Thumbnail directory [%default]')
parser.add_option('-v', '--verbose', dest='verbose',
                  action='count', default=default['verbose'],
                  help='Be verbose (may be used more than once) [%default]')
opts, args = parser.parse_args()

def error(msg):
    print >> sys.stderr, msg

def is_netpath(path):
    """Poor man's test for common network filesystem paths."""
    for netpath in ('/net', '/smb'):
        if path.startswith(netpath):
            return True
    return False

thumbs = [os.path.join(path, f) for path, dirs, files in os.walk(opts.thumbdir)
          for f in files if 'normal' in path and f.endswith('.png')]

bad = {}
skipped = {}
for thumb in thumbs:
    try:
        thumburi = Image.open(thumb).info['Thumb::URI']
    except KeyError:
        error('No "Thumb::URI" field in %s' % thumb)
        continue
    except IOError, error:
        error('%s: %s' % (error.strerror, thumb))
        continue

    scheme, netloc, path, query, fragment = urlsplit(thumburi)

    if scheme != 'file':
        continue

    path = unquote(path)

    if is_netpath(os.path.realpath(path)) and opts.skip_netpaths:
        skipped[thumb] = path
        continue

    if not os.path.exists(path):
        bad[thumb] = path
        if not opts.dryrun:
            try:
                os.remove(thumb)
            except OSError, e:
                error('Failed to remove "%s": %s' % (thumb, e.strerror))
                continue

print 'Total thumbnails:   %d' % len(thumbs)
print 'Bad thumbnails:     %d' % len(bad)
print 'Skipped thumbnails: %d' % len(skipped)

if opts.verbose:
    for thumb, path in sorted(bad.items(), key=lambda x: x[1]):
        print '%s -> %s' % (thumb, path)

-- 
Todd        OpenPGP -> KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When I think about all the crap I learned in high school ... it's a
wonder I can think at all.
    -- Paul Simon

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 542 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/fedora-list/attachments/20091028/a1ab9c21/attachment-0001.sig>


More information about the fedora-list mailing list