Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I find directories within directories using : parse_dir(`ls -l`)

( #109269=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie hkoza on Aug 31, 2001 at 06:24 UTC
Q&A  > directories


Description:

I am trying to search for directories within a given directory. If I use the above function, then it only returns me the files within a directory. I am supposed to write a function where I could pass the directory name and it will return a list of directories within.. Thanks, Hardik

Answer: How do I find directories within directories using : parse_dir(`ls -l`)
contributed by japhy

If using parse_dir isn't a hard requirement, you can read directories using the normal, built-in means: readdir.

opendir DIR, $path or die "can't read $path: $!"; @dirs = grep -d "$path/$_", readdir DIR; closedir DIR;
Answer: How do I find directories within directories using : parse_dir(`ls -l`)
contributed by toolic

If you could use some other module, File::Slurp provides a read_dir function which can be used as follows.

read_dir returns a list of directory contents.

grep -d selects only directories (filtering out files).

use File::Slurp; my @dirs = grep { -d "$dir/$_" } read_dir($dir);

To include the . and .. special directories, add the keep_dot_dot option:

my @dirs = grep { -d "$dir/$_" } read_dir($dir, keep_dot_dot=>1);
Answer: How do I find directories within directories using : parse_dir(`ls -l`)
contributed by Codmate

You could hand-roll it as follows.

Use ls to get a list of directories...

chomp (my @dir = `ls $path`);
...and then test each entry to see whether it is a directory:
my @list_of_directories = grep { -d "$path/$this_entry" } @dir;
Answer: How do I find directories within directories using : parse_dir(`ls -l`)
contributed by toolic

Assuming you're using the parse_dir function from the File::Listing module...

The following will populate the @dirs array with the names of the sub-directories in the current directory (excluding all hidden directories which begin with the dot character):

use File::Listing qw(parse_dir); my @dirs = map { my( $name, $type ) = @$_; $type eq 'd' ? $name : () } + parse_dir(`ls -l`);

To include 'dot' directories (except for the special . and .. directories), you'd simply use ls -la:

my @dirs = map { my( $name, $type ) = @$_; $type eq 'd' ? $name : () } + parse_dir(`ls -la`);

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (9)
    As of 2014-07-11 08:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (220 votes), past polls