Find the answer to your Linux question:
Results 1 to 5 of 5
Hilow every1, yes i am back again with a challenge (something that i do not know). How do we use perl to list out all the files in a directory ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    22

    use perl to recursive directories and list out all files


    Hilow every1,

    yes i am back again with a challenge (something that i do not know). How do we use perl to list out all the files in a directory and subdirectories?

    i am able to do it in bash but i couldnt get it done with perl.

    Anyone can help?Thanks...

  2. #2
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    Here is a routine that'll recurse a given directory (perl script.pl /path/to/search/).

    Code:
    #!/usr/bin/perl -w
    
    use strict;
    
    sub recurse($) {
      my($path) = @_;
    
      ## append a trailing / if it's not there
      $path .= '/' if($path !~ /\/$/);
    
      ## print the directory being searched
      print $path,"\n";
    
      ## loop through the files contained in the directory
      for my $eachFile (glob($path.'*')) {
    
        ## if the file is a directory
        if( -d $eachFile) {
          ## pass the directory to the routine ( recursion )
          recurse($eachFile);
        } else {
    
          ## print the file ... tabbed for readability
          print "\t",$eachFile,"\n";
        }
      }
    }
    
    ## initial call ... $ARGV[0] is the first command line argument
    recurse($ARGV[0]);
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  3. #3
    Just Joined!
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    22
    hi sykkn,

    may i know what does the following do?

    my($path) = @_;

    i do know about $_. this @_ i have not come across.

  4. #4
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    @_ is a special variable that holds the arguments of a subroutine.

    this is why you don't see this in perl: sub newSubroutine($arg1,$arg2);

    instead you can do the following:
    Code:
    #!/usr/bin/perl -w
    
    use strict;
    
    sub newSubroutine {
      for my $eachArg (@_) {
        print $eachArg,"\n";
      }
    }
    
    newSubroutine('some','random','number','of','arguments');
    so it's very similar to $_ only it's used with lists.
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  5. #5
    Just Joined!
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    22
    ok thanks for the info

    i check out the script in detail and get back to you.

    thanks alot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •