#keep based on pattern match opendir(DIR, $destinationDirectory) or die $!; while(my $file = readdir(DIR)){ next if -d $file;#skip directory entries unlink unless $file =~ /$pattern/; } #### #keep based on a prepopulated hash %prepopulatedHash =(FILE1 => 1, FILE2 => 1, FILEN => 1); opendir(DIR, $destinationDirectory) or die $!; while(my $file = readdir(DIR)){ next if -d $file;#skip directory entries unlink unless exists($prepopulatedKeepHash{$file}; }