Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

how to get the all the subdirectories path from the directory

by sen (Hermit)
on Mar 09, 2006 at 07:13 UTC ( #535340=perlquestion: print w/replies, xml ) Need Help??
sen has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I have to get path of all the subdirectories, in a particular directory. for example,

/aa/ /aa/abc/sam.txt /aa/abc/df/sample.txt /aa/abc/ef/ /aa/abc/ef/gh/ ....

Pls advice.

Thanks in advance.

Replies are listed 'Best First'.
Re: how to get the all the subdirectories path from the directory
by gube (Parson) on Mar 09, 2006 at 07:48 UTC
    Hi try this,
    use File::Find; find(\&file_names, "/home/gube/"); sub file_names { print "$File::Find::name\n" if(-f $File::Find::name); }
    Update : If you want to find only the directories use this code:
    use File::Find; find(\&dir_names, "/home/gube/"); sub dir_names { print "$File::Find::dir\n" if(-f $File::Find::dir,'/'); }
    Regards, Gube.
      I agree with Gube about using File::Find.
      But you don't need to bother writing the code, just use find2perl:

      find2perl . -type d

      will generate the code for you:

      you can use 'find2perl' to convert 'find' syntax to perl code.


      I don't think those do what you think they do. Here are some options:

      use File::Find; #print all entries at or below starting dir: find ( sub { print $File::Find::name,"\n" }, '.' ); #print all directories at or below starting dir: find ( sub { next unless -d; print $File::Find::name,"\n"; }, '.' ); #print all entries, trailing directories with a '/': find ( sub { print $File::Find::name,(-d $_ ? '/' : ''),"\n" },'.' );
      A collection of thoughts and links from the minds of geeks
      The Code that can be seen is not the true Code
      I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: how to get the all the subdirectories path from the directory
by Praveen (Friar) on Mar 09, 2006 at 07:27 UTC
Re: how to get the all the subdirectories path from the directory
by rinceWind (Monsignor) on Mar 09, 2006 at 08:45 UTC

    If you are looking at alternatives, you might like to try my File::Wildcard, which gives you an object that you repeatedly call $obj->next on, which returns each file name in turn.

    You have control of the sort order of each directory, the ellipsis ordering, i.e. whether you want to see subdirectories first or afterwards, and you can use a regex to filter the list and give you just the files that you want. You can even use $1, $2, etc. captures in the regex to construct a array of new filenames.



    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Re: how to get the all the subdirectories path from the directory
by davidrw (Prior) on Mar 09, 2006 at 13:54 UTC
    Another alternative is File::Find::Rule:
    use File::Find::Rule; my @list = File::Find::Rule->in('/aa'); # OR my @dirs_only = File::Find::Rule->directory()->in('/aa');
Re: how to get the all the subdirectories path from the directory
by leocharre (Priest) on Mar 09, 2006 at 16:06 UTC

    If you're running this on a linux box, you could use backticks.

    my @dirs = split(/\n/,`find /from/this/place/on -type d`);

    Nice thing about this is that you are using linux find, you can specify a lot of options, like formatting the output to get inode, timestamp, you can sort them by date, whatever.

    You may want to do a man find from your shell prompt- it would show you the options.

    You have to be aware that using backticks is more of a temporary script thing, for throw-away code. There are security and i guess portability issues with backticks.

      Not just linux, of course; any unix (sunos, bsd, macosx, ...) -- and of course a few different ports of unix tools (including a fully function "find" utility) ported to all versions of ms-windows/dos (gnu/cygwin, att research labs, ...)

      As for security concerns, you can do:

      open( $fh, "-|", "find", "/start/path", "-type", "d" ); while (<$fh>) { ... }
      That's as portable as perl 5.8.x and the command-line "find" util (i.e., runs anywhere). I actually prefer using this approach over File::Find and its derivatives, because the perl modules tend to go a lot slower on really big directory trees, whereas the compiled "find" util is as fast as you can get.
        We can use the @all_files = `find . -depth` ; // From the current directory @all_files = `find /path/ -depth` ; // To get all the paths from the path . Regards, Sravan
      Excellent solution, it worked for me thanks for the tip!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://535340]
Approved by gube
[erix]: choroba ah, you should have sprinted 7 s off that :)
[erix]: (but then there's always a next (im)possible goal)
Corion avoids running by starting on time
[choroba]: my phone showed me 1 hour 10 km which was my goal, it changed a bit after uploading to the website
[erix]: choroba I believe you ;)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2017-05-24 12:52 GMT
Find Nodes?
    Voting Booth?