Hi all,

Once again I am here with a simple problem which is hard enough to solve by myself:

I got the following keys in an array:

20110225104849
20110225104833
20110225104848

And following files in a folder:

google_20110225091600.7z
google_20110225091622.7z
google_20110225100306.7z
google_20110225100410.7z
google_20110225104833.7z
google_ready_20110225100410.txt
google_ready_20110225104833.txt
Yahoo_20110225091639.7z
Yahoo_20110225100320.7z
Yahoo_20110225100424.7z
Yahoo_20110225104849.7z
Yahoo_ready_20110225100424.txt
Yahoo_ready_20110225104849.txt
Community_20110225091637.7z
Community_20110225100318.7z
Community_20110225100422.7z
Community_20110225104848.7z
Community_ready_20110225100422.txt
Community_ready_20110225104848.txt

I should unlink/delete all the files from the folder except those which is/are carrying the keys in the filename.

Can I use a for loop? for match and delete? Or any handy shortcut?

rm `ls | egrep -v "(20110225104849|20110225104833|20110225104848)"`

do { unlink if ( !/20110225104849|20110225104833|20110225104848/ ) } for @your_files;

do { unlink if ( !/20110225104849|20110225104833|20110225104848/ ) } for @your_files;

I like Muthu's answer and it works, but BE CAREFUL when testing it that you glob the desired directory into your array. You don't want to delete all the files in whatever your current working directory happens to be!

#!/usr/bin/perl
use strict;
use warnings;
use 5.006;

my @your_files = <temp/*>;

#Comment out the following line the first time you test this.
#You want to make sure that @your_files contains files from the [I]desired[/I] directory.
#Otherwise you may delete all files in some other directory!!!
#do { unlink if ( !/20110225104849|20110225104833|20110225104848/ ) } for @your_files;

print "$_\n" foreach @your_files;

Thanks everybody!
Before closing this thread I would like to ask one more thing.

How the scenario would be handled if the keys are supplied in an array, like:

my @keys = ("20110225104849","20110225104833","20110225104848");

Regards,
Boshu

#!/usr/bin/perl
use strict;
use warnings;
use 5.006;

my @your_files = qw(google_20110225091600.7z
google_20110225091622.7z
google_20110225100306.7z
google_20110225100410.7z
google_20110225104833.7z
google_ready_20110225100410.txt
google_ready_20110225104833.txt
Yahoo_20110225091639.7z
Yahoo_20110225100320.7z
Yahoo_20110225100424.7z
Yahoo_20110225104849.7z
Yahoo_ready_20110225100424.txt
Yahoo_ready_20110225104849.txt
Community_20110225091637.7z
Community_20110225100318.7z
Community_20110225100422.7z
Community_20110225104848.7z
Community_ready_20110225100422.txt
Community_ready_20110225104848.txt);

my @keys = ("20110225104849","20110225104833","20110225104848");
my $pattern = join '|', @keys;# Result: 20110225104849|20110225104833|20110225104848
print "You do not want to delete files matching this pattern:\n$pattern\n\n";
print "You DO want to delete the following files:\n";

#For testing I substituted 'print' for 'unlink'
#When you're sure it works correctly replace with
#do { unlink if ( !/$pattern/ ) } for @your_files;
do {print "$_\n" if ( !/$pattern/ )} for @your_files;

That's a smart solution for me.
Thanks David, Muthu and all others. I am closing this thread as solved