use warnings; use strict; my $file2 = '/tmp/foo'; print "Enter filename to search\n"; chomp( my $file = ); if ( -e $file ) { die "File already Exists!\n"; } unless ( -e $file ) { my @appfiles = glob "*"; open my $fh, '>>', $file2 or die $!; print $fh "List of all the files in the directory:\n"; print $fh "@appfiles\n"; close $fh; }