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

Hello,

I would like to erase all the files in the directory a/b but those with a .zip suffix.

Since:

unlink glob('a/b/*.zip');

does deletes all the files with .zip suffix, I though that:

unlink glob('a/b/!(*.zip)');

could erase all the files but those with .zip suffix.

But it does not work: it deltes no files.

Any suggestion please?

Replies are listed 'Best First'.
Re: Beginner question about files deletion
by Corion (Patriarch) on Feb 07, 2012 at 11:24 UTC

    Maybe you want to look into how glob works? It does not work on make-believe. Personally, I recommend using bsd_glob() from File::Glob instead as it has much, much saner globbing rules. Also see glob (7) for a brief description of (some) globbing patterns.

Re: Beginner question about files deletion
by JavaFan (Canon) on Feb 07, 2012 at 12:40 UTC
    Sometimes, the easy solution is to not use perl.
    $ find a/b -maxdepth 1 '!' -name '*.zip' -type f -exec rm '{}' ';'
    Or, from Perl:
    unlink `find a/b -maxdepth 1 '!' -name '*.zip' -type f`;
    If you're silly enough to put newlines in your filenames, use
    unlink split /\x00/, `find a/b -maxdepth 1 '!' -name '*.zip' -type f - +print0`;
Re: Beginner question about files deletion
by choroba (Cardinal) on Feb 07, 2012 at 11:45 UTC
    What about filtering the files with grep?
    unlink grep ! /\.zip$/, glob 'a/b/*';
    (Untested.)