Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How would I list all files from directories and sub-directories? Here is my code but it only lists from the main directory Im using. I need to find out all the files in the sub-directories having it add the additional directory to the printout. Thanks again...
#!/usr/bin/perl my $dir = "/cdw/home_dir/s006258/"; #Opening directory and file to compare retention files die("Cannot open /cdw/home_dir/s006258/list_of_dirs.txt.") unless(open(FILE, ">/cdw/home_dir/s006258/list_of_dirs.txt")); # read all entries from dir and skip '.' and '..' directories opendir (DIRHANDLE, $dir) or die "can't open $dir: $!"; my @list=grep !/^\.\.?\z/, readdir DIRHANDLE; # now we sort the list on directory basis foreach my $file (sort {-d "$dir/$b" <=> -d "$dir/$a"} @list) { print FILE "$file\n" if -d "$dir/$file"; print FILE "$dir"."$file\n" if -T "$dir/$file"; } closedir (DIRHANDLE); close(FILE);

Replies are listed 'Best First'.
Re: List all Dirs, subdirs and files
by Tanktalus (Canon) on Jul 18, 2005 at 22:22 UTC

    Check out File::Find. Way easier than rolling your own.

    my $dir = "/cdw/home_dir/s006258"; my @dirs; my @files; sub do_stuff { if ( -d $_ ) { push @dirs, $File::Find::name; } else { push @files, $File::Find::name; } } find(\&do_stuff, $dir); foreach my $name (@dirs, @files) { print FILE $name, "\n" if -d $name or -T $name; }

    Warning: completely untested.

    Update: I had the wrong case on the $File::Find::name variable. Fixed.

      So will this work if I want everything not just a specific file?

        I'm not sure what you're seeing that makes it look like this only gets a specific file? I mean that in an honest manner - there is something in my untested code that made you ask this question, and I'd like to know what it is. On one hand, it could be the key to helping you understand, on the other hand, it could be a blatant error in my untested code.

Re: List all Dirs, subdirs and files
by kwaping (Priest) on Jul 18, 2005 at 23:17 UTC
    Can I ask what your goal and operating system are? I'm wondering because there might be a unix command that'll do it without needing to write a script. (Heresy!)
      Running on a Sun Solaris, not sure what version. Could I use the command 'Find' without trying to find a specific word and to find everything in every directory?
        Yes! I use it all the time. Just type this:
        find /directory/here
        to list everything recursively and with the search path prepended. :)
Re: List all Dirs, subdirs and files
by murugu (Curate) on Jul 19, 2005 at 07:31 UTC
Re: List all Dirs, subdirs and files
by zentara (Cardinal) on Jul 19, 2005 at 11:37 UTC
    Here's an idea.
    #!/usr/bin/perl use warnings; use strict; use File::Slurp::Tree; # The tree datastructure is a hash of hashes. The keys of # each hash are names of directories or files. Directories # have hash references as their value, files have a scalar # which holds the contents of the file. my $dir = shift || '.'; my %tree; my $tree = slurp_tree($dir); my $depth = 0; print "$dir\n"; print_keys($tree); sub print_keys { my $href = shift; $depth++; foreach ( keys %$href ) { print ' ' x $depth, "--$_\n"; print_keys( $href->{$_} ) if ref $href->{$_} eq 'HASH'; } $depth--; }

    I'm not really a human, but I play one on earth. flash japh