Solved

Perl Program listing files in a directory

Posted on 2008-06-21
4
793 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

813 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