Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Hard disc pruner

by lhoward (Vicar)
on Jul 07, 2000 at 17:23 UTC ( [id://21465]=note: print w/replies, xml ) Need Help??


in reply to Hard disc pruner

I'd definitely do it with File::Find. Since you already have an array of files to keep, I'd convert this to a hash (paths/names to keep as they keys). Then use File::Find to loop through the filesystem and check to see if each file that File::Find locates is in the hash. If it is not in the hash, delete it.

Here's a simple codeup of my recomendation.... You should test it THOUROUGHLY before uncommenting the unlink line..

#!/usr/bin/perl -w use strict; use File::Find; my %keep; # load %keep with the filenames you want to keep... # $keep{/bin/sh}=1; # $keep{/sbin/ifconfig}=1; # etc.... find(\&checkfile,'/'); sub checkfile{ my $fname="$File::Find::dir/$_"; if(!defined $keep{$fname}){ print "deleting $fname\n"; # unlink $fname; } }

Replies are listed 'Best First'.
RE: Re: Hard disc pruner
by mikfire (Deacon) on Jul 08, 2000 at 03:47 UTC
    Danger, Will Robinson, Danger! I really hope there are no directory names in that list of yours. Unlinking directories is usually a very bad idea - problems that even fsck has a hard time fixing can result.

    Having said that and realizing the caffiene/sleep ratio is far too high, lets have some fun. Can we do this in one line? Why, yes!

    @dead = grep { ! defined( $keep{$_} ) && ( -d $file ? rmdir : unlink ) && $_ } @files;
    which will not call unlink on directories and return a list of the files deleted. It does assume that you have done a depth-first walk to generate the file names though. Now, this is useful, but I could generate the "expected to be blown away" list easier. What I would be interested in is the files that didn't get blown away.
    @dead = grep { ! defined( $keep{$_} ) && ( ( -d $file ? rmdir : unlink ) || $_ ) } @files;
    Well this is cute, but why couldn't we blow the file away?
    @dead = grep { ! defined( $keep{$_} ) && ( ( -d $file ? rmdir : unlink ) || "$_:$!" ) } @files;
    On second thought, follow lhoward's suggestion - this code is dangerous and will likely get you cursed by anybody having to maintain it.

    Mik Firestone ( perlus bigotus maximus )

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://21465]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found