in reply to unlinking multiple files at once

you can use a glob, like unlink <123*.txt>; or unlink glob(123*.txt);
This can be a neat way to shoot yourself in the foot.

Update In answer to your question, make sure you have as specific a glob as possible; don't use 123*.* if you want to keep (e.g.) 123foobar.txt. If you have many files which would match \d{3}, but only want to delete a portion (say, delete only 123*.*, but keep 456*.*), be wary of Fletch's answer.

The first time you create a glob, you might want to substitute print for unlink, just till the kinks are worked out. If you still have concerns, try using File::Copy to copy/ move the file instead of deleting it. These test methods will ensure any mistakes don't have you scrambling for backups... You do have backups, right? :-)

Replies are listed 'Best First'.
Re: unlinking multiple files at once
by krose (Initiate) on Oct 03, 2001 at 02:39 UTC
    Is there a better way to delete multiple files at once so as not to hurt my feet?
      Yes, and strategy is the name of the game.

      You tend (%90+ time) to "shoot yourself in the foot" when you try to do too many things at once (like run a marathon, whilst talking on your cellphone, and balancing a book on your head, with your pants around your ankles, orr, drive whilst carrying on a conversation with someone in the back seat and someone on the phone, all whilst eating a giant sandwich)

      So, what you want to do, what you need to do, if you wish to be able to walk after you run your program, is gather a list of the files in whatever directory you wish to play in, and this can be accomplished using File::Find or simply with the readdir function (I suggest File::Find if you need to recurse infinetly deep... for most situations)

      After you have a list of *potential* deletion candidates, you need to weed out the ones you want (or if it's easier, the ones you wish to keep).

      You can either push the qualifying candidates onto an array, and unlink all of them at once, or just delete them when you detect one.

      I'd personally use File::Find for the job, and *qualify* and unlink a candidate in one function (commonly called &wanted), especially if you got lots of subdirectories and stuff (cause if you don't have a *full* path, you have to chdir, which File::Find does for you).

      Here is some authentic looking pseudocode.

      #!/usr/bin/perl -wT use strict; use File::Find; undef %ENV; &find(\&wanted,'/some/path/stuff'); print "all done\n"; exit(1); ## s u b l a n d sub wanted { my $fn = $_; # filename if ( -f $fn) # it's a file { if ( /somepattern/ and /somepattern/ and not /somepatter/) { printf "unlinking $fn (1 == success) ... %s\n", unlink $fn; } } }

       
      ___crazyinsomniac_______________________________________
      Disclaimer: Don't blame. It came from inside the void

      perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"