Solved

Perl Program listing files in a directory

Posted on 2008-06-21
4
803 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl 101 11 85
Question about @INC variable in perl 1 68
Linux rename files by reordering dates in the file name 6 39
add a syntax to a csv file 8 99
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

749 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