[RndTbl] Montana: recursive delete based on prefix

Trevor Cordes trevor at tecnopolis.ca
Thu Apr 16 14:50:46 CDT 2009

Here's my program that will easily, recursively delete all files with a
certain prefix.  I use .# for recoverable delete (if anyone wants my
matching rm command, I call "r", let me know).  Montana, you'll want to
switch file_prefix to '._'.  You'll also want to set $not_trevor=1 so
it doesn't do some extra features I use with my r.

#!/usr/bin/perl -w
# v2.1 by Trevor Cordes

# set the first 2 vars below first;
# you may need to change the perl, bash path references
# recursively deletes files that have a certain prefix (eg .#);
# recurses from the current dir if no args given, or from the given
#   directory arg(s);
# dir args cannot have spaces (currently, but would be easy to fix);
# should be safe for recursed files/dirs with spaces/newlines in name;
# deletions are recursive! so if a dir has the prefix then all sub-
#   dirs/files will be rm'd too!

# -o days	only expunge files r'd more than <days> days ago
# -q            quiet: don't output normal one-line-per-file status

$file_prefix='.#'; # set to the file prefix you want to delete, BE CAREFUL! 
$not_trevor =0; # set to 1 if you are not me
		# and not using this to expunge r'd files
		# disables the -o option


use Getopt::Std;

$dir=join(' ', at ARGV) if @ARGV;


$olderthan=($opts{'o'} and $opts{'o'}>0)
  ? time-($opts{'o'}*86400)
  : time+100000; # fudge a time way in the future

print "Expunging <$dir>...\n" if !$quiet;

die if !$file_prefix; # sanity check!

system "find $dir -depth -xdev -name '$file_prefix*' -print0 "
   :"| perl -n0e 'print if !/~(\\d+)~\\d+~\\d+\\000?\$/ or \$1<$olderthan' " 
 ."| xargs --no-run-if-empty --null /bin/rm -r ".($quiet?'':'-v');

More information about the Roundtable mailing list