Scripts to list Directory file & contents

Posted on 1999-01-24
Last Modified: 2010-05-18
I'm looking for perl script to list the files in a directory and the contents of the files. The Contents could be driven from another file with single line discriptsion.
 If you know of something like this please advise.
Question by:dpdr
  • 2
  • 2
LVL 84

Expert Comment

ID: 1210282
opendir(DIR, $some_dir) or die "can't opendir $some_dir: $!";
while( defined($_=readdir(DIR)) ){
    print "$_\n";
#I'm not sure I understand your second sentence.

Expert Comment

ID: 1210283
could use @file_stats = stat $file;
if you are talking about file att's such as size, mod date etc.

( $dev, $ino, $nlink, $uid, $gid, $rdev, $size,
  $atime, $mtime, $ctime, $blksize, $blocks ) = stat $filename


Author Comment

ID: 1210284
I guess I should have been a little clearer. I'm tryint to creat  a listing of documents in a directory. That have the document numbers and a comment line 25 chars. The output of the listing would be HTML and built on the fly. So once the directory was built files added to it would be pickup when someone would access it. then an edit script could be wriiten to add information to a file with the comment data in it.

Accepted Solution

tpryor earned 400 total points
ID: 1210285
Got ya,

we do this at work a lot.

For speed we index the directory first into a DBM or flat text file usally nightly but we can maually execute it also.

Then we have a front end script that reads the index file and reports the results to the screen.

First decide on what tags you will use and load them into your files.

# next lets read the directory

opendir( DIR, "$dir_path" );

# open every file in the dir

foreach $file ( readdir DIR ) {
      next if ( $file =~ /^\./ ); # skip the . .. files on unix
      open( FILE, "$dir_path:$file" ) || die "couldn't open $file: $!";

# look through each file for our tags
      while ( $line = <FILE> ) {
            last if ( $line =~ /^<!---End of Tags---->/ );
# tags look like this <!--name=value-->
            if ( $line =~ /^<!--/ ) {
                  $line =~ /^<!--(.*[^\=])=(.*[^\-])-->/;

# store tags in array to print to index file later
                  push( @{$tags{"$file"}},  [$1, $2] );
close FILE

# now lets format our tags for index file

foreach $file ( sort keys %tags ) {

       $index .= "$file|";
        foreach $array ( @{$tags{"$file"}} ) {

# we will store our values only in the $index scalar

               $index .= "@$array[1]|";
   $index .= "\n";
closedir DIR;

# now we will write our tags to a flat index file

open ( INDEXFILE, ">$path_to_index_file" );
print INDEXFILE $index;

########### end #############

the file will look like this


if we had three html files with these tags


NEXT you would have a script read the index file and output the results to html.

it is faster and easier to deal with

######### display code ##########

open ( INDEX, "$path_to_index_file" );

# read one record at a time
while( $record = <INDEX> ) {
# split the record up into its parts
@fields = split( /\|/, $record );
# print out with your html
print join( ",", @fields) . "<br>";
close INDEX;
# thats it

########## end ##########

hope that helps


Author Comment

ID: 1210286
Thanks that looks like What I need.
I got it to do some stuff in ksh but perl should be faster.

From what you wrote I should be able to modify it if needed.
But starting in perl would mean learning it.


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
Syslog text file into database or into .CSV 19 159
pattern matching in perl 2 107
Migrate OTRS to newest version. 1 382
Perl tar error 8 67
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

827 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