use strict; my $file_location = shift; open(FH,">","log.txt") or die "unable to open file log.txt"; my @files = <$file_location/*>; foreach my $file (@files){ system("rm $file"); unless( $? == -1 ){ print FH "\n$file is deleted.."; } else{ print FH "\n$file is not deleted.. $!"; } } close FH;