Delete .xvpics directories safely

This is a Perl script to carefully delete all .xvpics directories.


# Delete old .xvpics directories containing misleading file names. The
# files are some kind of thumbnail format, but they are given names
# ending in .jpeg etc., which they don't actually contain those kinds
# of files.

use warnings;
use strict;
use utf8;
use FindBin '$Bin';

# Use "locate" to find the directories.

my @xvpicsdirs = `locate ".xvpics" | grep -v ".xvpics/" `;

for my $dir (@xvpicsdirs) {
    chomp $dir;
    # If we run this twice, the directories are all gone.
    if (! -d $dir) {
    print "$dir\n";
    chdir $dir;

    my @files = <$dir/*>;
    for my $file (@files) {
        # Carefully delete only files which are thumbnail files.
        if ($file =~ /\.jpe?g$|\.gif$|\.png$/) {
            unlink $file or die $!;
        else {
            print "Cannot remove $file\n";
    # If the above file deletion fails, due to non-thumbnail files in
    # the directory, the following won't accidentally delete the good
    # files, but instead print a directory not empty error, which is
    # what we want - we don't want to destroy the contents if the
    # directory contains anything useful.
    system ("rmdir $dir");


Copyright © Ben Bullock 2009-2024. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer