954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Conditional unlink

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?

boshu
Light Poster
48 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 
rm `ls | egrep -v "(20110225104849|20110225104833|20110225104848)"`
masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
do { unlink if ( !/20110225104849|20110225104833|20110225104848/ ) } for @your_files;
k_manimuthu
Junior Poster in Training
93 posts since Jun 2009
Reputation Points: 55
Solved Threads: 24
 
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 <em>desired</em> 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;
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

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

boshu
Light Poster
48 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 
#!/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;
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

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

boshu
Light Poster
48 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: