Re: how to get the all the subdirectories path from the directory
by gube (Parson) on Mar 09, 2006 at 07:48 UTC
|
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. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
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" },'.' );
| [reply] [Watch: Dir/Any] [d/l] |
Re: how to get the all the subdirectories path from the directory
by Praveen (Friar) on Mar 09, 2006 at 07:27 UTC
|
| [reply] [Watch: Dir/Any] |
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.
Enjoy
--
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)
| [reply] [Watch: Dir/Any] |
Re: how to get the all the subdirectories path from the directory
by davidrw (Prior) on Mar 09, 2006 at 13:54 UTC
|
use File::Find::Rule;
my @list = File::Find::Rule->in('/aa');
# OR
my @dirs_only = File::Find::Rule->directory()->in('/aa');
| [reply] [Watch: Dir/Any] [d/l] |
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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
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. | [reply] [Watch: Dir/Any] [d/l] |
|
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
| [reply] [Watch: Dir/Any] |
|
Excellent solution, it worked for me thanks for the tip!
| [reply] [Watch: Dir/Any] |