in reply to Delete files matching pattern

Use the shell, Luke.
find . -type f -name '.*se\d\d\d\dab.*' -exec rm {} \; # Or ... find . -type f | grep '.*se\d\d\d\dab.*' | rm

If you have to do it in Perl, that's about the best you're going to get.


  • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
  • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"

Replies are listed 'Best First'.
Re^2: Delete files matching pattern
by mda2 (Hermit) on May 09, 2005 at 20:47 UTC
    You can use find2perl to make Perl code, and change to your needs:
    find2perl . -type f -name '.*se\d\d\d\dab.*' -exec rm {} \;
    #! /usr/bin/perl -w eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0; #$running_under_some_shell use strict; use File::Find (); # Set the variable $File::Find::dont_use_nlink if you're using AFS, # since AFS cheats. # for the convenience of &wanted calls, including -eval statements: use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; sub wanted; # Traverse desired filesystems File::Find::find({wanted => \&wanted}, '.'); exit; sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _ && /^\..*se\d\d\d\dab\..*\z/s && (unlink($_) || warn "$name: $!\n"); }

    --
    Marco Antonio
    Rio-PM

Re^2: Delete files matching pattern
by bofh_of_oz (Hermit) on May 09, 2005 at 15:40 UTC
    Sorry, should've included it in the post - I'm using the script on Win platform, so no shell option for me...

    Is it not possible to say something along the lines of

    if (-x /se\d{4}ab/) {unlink _;}

    Of course the one I just wrote doesn't work but I mean, the general idea of combining it like this... I realize my ideas might look silly but my eyes still glaze over when I see regexps...

    --------------------------------
    An idea is not responsible for the people who believe in it...

      You're not far off, actually:

      foreach (@files) { unlink $_ if (/se\d{4}ab/i && -x) }
      would work if @files contains the list of all the files you want to check. A quick note -- others picked up on it in code, but didn't mention explicitly -- the i at the end of the regex will match in a case-insensitive manner, which is faster than invoking the lc() on each potential filename as you do in your original code.

      The Eightfold Path: 'use warnings;', 'use strict;', 'use diagnostics;', perltidy, CGI or CGI::Simple, try the CPAN first, big modules and small scripts, test first.