#!/usr/bin/perl use strict; use warnings; # find files that match the naming convention my @files = `find . -name ".saves*~"`; foreach ( @files ) { chomp; # hold onto filename, and extract creator's pid / # start of pattern match ( # begin storing into $1 .* # store any number of any character... - # ...followed by a hyphen... ( # begin storing into $2 \d # ...any digit... + # ...as many as we can grab... ) # stop storing into $2 - # ...followed by another hyphen .* # ...followed by any number of any character... ) # stop storing into $2 $ # end of the line, bub /x; # / to terminate regex, x to allow comments my ( $filename, $creator_pid ) = ( $1, $2 ); # Check process stack for the creator's pid, storing command result my $command = "ps -e -o pid | grep $creator_pid | grep -v grep"; my $command_result = `$command`; # if the command result is positive, leave $filename alone... # ... otherwise, remove $filename if ( $command_result ) { print "+ $filename\n"; } else { print "- $filename\n"; `rm $filename`; } }