Solved

Perl Program listing files in a directory

Posted on 2008-06-21
4
786 Views
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.
0
Comment
Question by:cntech
  • 3
4 Comments
 
LVL 48

Expert Comment

by:Tintin
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?
0
 
LVL 14

Expert Comment

by:agriesser
ID: 21838543
There are several implementations of such a program at the following link:

http://www.nntp.perl.org/group/perl.beginners/2007/06/msg92313.html
0
 
LVL 14

Expert Comment

by:agriesser
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.
#!/usr/bin/perl

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

HELP
 

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

0
 
LVL 14

Accepted Solution

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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.…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

19 Experts available now in Live!

Get 1:1 Help Now