Perl v5.18.2 on Fedora 20

Uses modules. If these are not installed on your system see the article on installing cpan modules.


# This program will list the files in the current directory.
# The full path with filename will print to the screen
# An output file "list.txt" will be created with just the file names

use strict;
use warnings;
use Cwd;
use File::Basename;

# Get current directory
my $dir = getcwd;

# Get a outfile (IO::File object) you can write to
my $outfile = 'list.txt';

# here 'open' the file, write to it with the '>>' symbol
open (FILE, ">> $outfile") || die "problem opening $outfile\n";

# Create an array of all the files in the directory using glob
my @filelist = glob( $dir . "/*");

# Print array to the screen
foreach (@filelist){
   print $_ . "\n";

# Use basename from the File::Basename module to write to the output file
# Note: You will also get the output file list.txt in the result
foreach (@filelist){
   print FILE basename($_) . "\n";

# close the file when there's nothing else to write to it