Perl Program listing files in a directory

Posted on 2008-06-21
Last Modified: 2008-07-12
I'm a beginner at creating perl programs.  How can I create a program that will list files in a directory and provide a user with 3 command line options.  They should be able to specify a directory, which I think you use the -d option, also they can display a long listing of the files, I think you use the -l and allow them select no options.  Also, when the files are listed they should be sorted by the filename.  I'm new to creating perl programs, not sure how to accomplish this.
Question by:cntech
  • 3
LVL 48

Expert Comment

ID: 21838489
Your specification is unclear.

You say there should be three command line options, but you have listed just 2.  What behaviour do you want when no options are selected?
LVL 14

Expert Comment

ID: 21838543
There are several implementations of such a program at the following link:
LVL 14

Expert Comment

ID: 21838552
I'll include the program here just in case the other site isn't reachable sometime in the future.

Please note: The code is (c) John W. Krahn.

use warnings;

use strict;

use Getopt::Std;

getopt 'hl', \my %opt;

exists $opt{ h } && die <<HELP;

usage: $0 [-h] [-l] [directory]

      -h    this message

      -l    long listing


my $dir    = shift || '.';

my $format = "%-25s %10s %-8s %-8s\n";

opendir my $dh, $dir or die "Cannot open '$dir' $!";

if ( exists $opt{ l } ) {

     printf $format, 'File Name', 'Size', 'Owner', 'Group'


else {

     print "File Name\n"


while ( my $file = readdir $dh ) {

     my ( $uid, $gid, $size ) = ( lstat "$dir/$file" )[ 4, 5, 7 ] or die 

"Cannot stat '$dir' $!";

     next unless -f _;

     if ( exists $opt{ l } ) {

         printf $format, $file, $size, scalar getpwuid $uid, scalar 

getgrgid $gid


     else {

         print "$file\n"



Open in new window

LVL 14

Accepted Solution

agriesser earned 500 total points
ID: 21924183
Did this script work for you?

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now