Sunday, September 27, 2009

dudley

Whenever I try to find a file on Linux/Ubuntu, I run into difficulties. Either my privileges don't match the file's hidden (!) location, or it's hiding among ten thousand other files, or Tracker's on strike, or whatever. And occasionally, what I need to do when I'm looking for a file is wipe it and all its idiot siblings off my system because they take up space.

Well, that's what the Linux find command is for. But find is a bear to use. So here's a compromise — I call it dudley, because the name is stupid and memorable, and because dudley is a mass murderer (of files!) which you have to respect.

Hmmm... If you use dudley you will also need srm, which you can get (in Ubuntu) by entering sudo apt-get install secure-delete at the command line, or by using Synaptic. I presume you already have perl in the normal location.

Don't Try This At Home, unless you've saved the script as dudley, chmod'd it to 755, and signed a pledge in your own pulsing arterial blood not to bother me with questions or consequences.

Running dudley in kill mode is a stupid thing to do. DRINK COFFEE before pressing Enter...



#!/usr/bin/perl
#
# usages:
# dudley
# dudley $DIR $TARGET
# dudley somedirectory partofsomefilename
# dudley -x somedirectory partialnameofmanyfilestokill
#
# sample calls:
# dudley /home/mydaughtersname/ *.mov
# dudley . vlcsnap
#
# dudley (i.e., this script) does horrible things to all references
# to $TARGET in the $DIR directory.
# I wrote it (on Mac OS X) so I could sponge up and wring out some
# stuff I don't want in my Fink /sw directory. Always requires sudo,
# to make the point that this is a merciless utility.
#
# 3 March 2003
# d.c.oshel

require "getopts.pl"; # good old Perl 4

# invoke with sudo if not root...

if ( $< != 0 ) # even in harmless cases, emphasizes that dudley is dangerous
{
exec( "sudo $0 @ARGV" ); # does not return
}

&Getopts('xq'); # implies shift if switches are found

# down to business...

$VERBOSE = $opt_q ? 0 : 1; # verbose is not quiet
$KILL = $opt_x;
$APP = $0;
$APP =~ s/[\.\/].*\///; # strip path from beginning of application name
$DIR = $ARGV[0];
$TARGET = $ARGV[1];

if ( $DIR eq "." ) {
$DIR = `pwd`;
chop( $DIR );
}

if ( "$TARGET" eq "" ) {

print "\nWarning: $APP is a mass murderer (of files and directories), so be careful!\n\n";
print "usage: $APP [options] DIRECTORY PATTERN\n";
print "options:\n";
print " -x Securely remove files that match PATTERN, using srm -r\n";
print " -q Suppress user-friendly remarks in simple listings\n\n";
#print "\nIf you need something more sophisticated, try (e.g.)\n";
#print " find DIRECTORY -iname PATTERN -exec srm -r -- {} \\;\n\n";

} else {

if ($KILL) {
print "Destroy all files like \"*$TARGET*\" in $DIR? [Yn] ";

LUP:

$ans = ;
chop($ans);

if ( "$ans" eq "n" || "$ans" eq "N" ) {
if ($VERBOSE) {print "No changes were made!\n";}
exit 0;
}
elsif ( "$ans" eq "" || "$ans" eq "y" || "$ans" eq "Y" ) {
if ($VERBOSE) {print "Deleting all like \"$TARGET\"s in $DIR ...\n";}
system( "find \"$DIR\" -iname \"*$TARGET*\" -print | xargs srm -r "); # -d is depth-first traversal (post-order)
if ($VERBOSE) {print "\n";}
}
else {
print "? [Yn] ";
goto LUP;
}
}

else {
if ($VERBOSE) {print "Looking for \"$TARGET\"s in $DIR ...\n";}
system( "find \"$DIR\" -iname \"*$TARGET*\" -print" ); # normal pre-order traversal
}
}

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home