krose has asked for the wisdom of the Perl Monks concerning the following question:

Can I use a wild card with unlink? I have a group of ten files that all start with the same three digit string. I want to delete each group at different times. Can I use a wild card to specifiy all files that match the beginning three digits?

Updated : boo_radley : title edited to remove conflict with unlink

Replies are listed 'Best First'.
unlinking multiple files at once
by boo_radley (Parson) on Oct 03, 2001 at 01:52 UTC
    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? :-)

      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;"

Re: unlink
by runrig (Abbot) on Oct 03, 2001 at 01:55 UTC
    Can I use a wild card with unlink?

    Indirectly, yes. unlink can take a list, so just supply it with the result of a glob (careful since unlink with no arguments unlinks $_):

    my @files = <*.tmp>; if (@files) { unlink @files or warn "Problem unlinking @files: $!"; } else { warn "No files to unlink!\n"; } # Or unlink or warn "Problem unlinking $_" for <*.tmp>;
    Update: I thought that unlink with an empty list would act the same as unlink with no arguments, but I was wrong, so boo_radley's answer is perfectly ok, except for the lack of error checking, if you care about such things :)
Re: unlinking multiple files at once
by Fletch (Bishop) on Oct 03, 2001 at 02:49 UTC

    Yet another way . . .

    opendir( CUR, "somedir" ) or die "Can't opendir: $!\n"; my @digit_files = grep /^\d{3}/, readdir( CUR ); closedir( CUR ); foreach( @digit_files ) { unlink "somedir/$_" or warn "Can't unlink somedir/$_: $!\n"; }
      In most cases, this is my preferred method because you have more control than a glob (you have the full power of Perl's Regular Expressions at your disposal). Although as boo_radley points out - this will delete all files beginning with 3 digits. The original question was regarding a specific set of three digits.

      And since you only want to delete files - not directories, you might want to change the grep:
      my $prefix = '123'; my @digit_files = grep { /^$prefix/ && -f } readdir( CUR );
      Update: fixed formatting and added why I prefer this method over globbing.

      Simon Flack ($code or die)
      $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
      =~y'_"' ';eval"die";print $_,lc substr$@,0,3;
Re: unlinking multiple files at once
by Zaxo (Archbishop) on Oct 03, 2001 at 07:20 UTC

    The unlink glob() form recommended by boo_radley is necessary if you want to interpolate values from variables in the argument. The diamond operator will not interpolate correctly for filename globs.

    Your foot is safe if the filename glob catches only the files you want. That is easier to review with globs than with true regexes, but care is still needed.

    After Compline,
    Zaxo