A tree program recursing the directory tree 
Author Message
 A tree program recursing the directory tree

Someone asked about subdirectory recursion, which caused me to finally
send my tree program whichs traverses the directory tree of all
directories below the current one, and prints out the size in kBytes of
the sum of the sizes of all files in each directory.

Lot's of options could be added to this program to make it into a
proper utility. Suggestions (and total rewrites) are welcome and
encouraged.

#!/usr/bin/perl
##############################################################################
#  Prints a subtree of all files under the current directory
#  including the sum of the size in kb of all files in each subdirectory.
#
#  Dov Grobgeld
#  Department of Chemical Physics
#  The Weizmann Institute of Science

#
#  Version 0.1
#  19 September 1991
##############################################################################

sub getSubDirs {

  $indent++;

  # Write the total size of the files in the current directory
  opendir(DIR, '.');

  $totdirsize=0;

    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
    $blksize, $blocks) = stat($_);
    $totdirsize += $size;
  }
  print " \[", $totdirsize/1000, "\]\n";

  #######################################################################
  # Get all the subdirectories and write their names to the output
  # For each subdirectory transverse its subdirectories unless
  #    1. The directory is a symbolic link
  #    2. We do not have sufficient priveleges to read the directory
  ########################################################################

  opendir(DIR, '.');

  closedir(DIR);

    print '  ' x $indent, $_;
    if (-l $_) { print " (L)\n"; }        # Symbolic link
    elsif (!(-x $_)) {print " (NA)\n"; }  # Subdirectory is Not Available
    else {
      chdir $_;
      &getSubDirs;
      chdir '..';
    }
  }
  $indent--;

Quote:
}

$#= "%.0f"; #output format for floating point numbers
$_= `pwd`;
chop;
print;
&getSubDirs;


Mon, 07 Mar 1994 21:37:02 GMT  
 A tree program recursing the directory tree
| Someone asked about subdirectory recursion, which caused me to finally
| send my tree program whichs traverses the directory tree of all
| directories below the current one, and prints out the size in kBytes of
| the sum of the sizes of all files in each directory.
|
| Lot's of options could be added to this program to make it into a
| proper utility. Suggestions (and total rewrites) are welcome and
| encouraged.

Well, for starters, you could simply use "find2perl" in the latest
distribution.  There's not much on it, but it's pretty easy to use.

        find2perl . -print | perl

is like:

        find . -print

and

        find2perl . -mtime +3 -ls -eval 'rename($_,"$_.OLD")' | perl

renames all files older than three days, appending ".OLD" to their
names.  Read the source for the other options.  (I thought I saw a
manpage on it once, but I don't see one now.)

Or, you could use my ftw package, as in:

require 'ftw.pl';
&ftw(".","each");
sub each {

        return unless -s > 1e6;
        print "$_ is greather than a million bytes!\n";

Quote:
}

Here's ftw.pl:

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#               "End of shell archive."
# Contents:  ftw.pl

PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'ftw.pl' -a "${1}" != "-c" ; then
  echo shar: Will not clobber existing file \"'ftw.pl'\"
else
echo shar: Extracting \"'ftw.pl'\" \(1684 characters\)
sed "s/^X//" >'ftw.pl' <<'END_OF_FILE'
X## ftw.pl rev 4.0
X
Xpackage ftw;
X
X# &ftw("path","function-name")
X# calls &function-name("path/file") for each name returned by the
X# equivalent of "find path -xdev -print"
X
Xsub main'ftw {

X
X       $fn =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
X       if (-d $path) {
X               &helper($path);
X       } elsif (-e $path) {
X               do $fn("$path");
X       }
X}
X
Xsub helper {

X
X       local($dev, $ino, $mode, $nlink) = stat($path);
X       local($_,*DIR);
X       opendir(DIR,$path) || die "Cannot open $DIR ($!)";

X       closedir(DIR);
X       $path = "" if $path eq "/"; # don't double the /!
X
X       if ($nlink == 2) {

X                       do $fn("$path/$_");
X               }
X       } else {

X                       $_ = "$path/$_";
X                       do $fn("$_"); # cannot pass $_ as lvalue
X                       next unless ! -l $_ && -d _ && -r _ && -x _;
X                       next if $dev != (stat(_))[$[+0]; # "-xdev"
X                       &helper("$_"); # recurse if directory
X               }
X       }
X}
X
Xpackage ftw_root;
X
X# &ftw_root("function-name")
X# calls &function-name("/file",stat("/file")) for each name
X# returned by the equivalent of "find / -fstype nfs -prune -o -print"
X# note that stat buffer _ is correct during the call (unlike &ftw() above)
X
Xsub main'ftw_root {

X       $fn =~ s/^([^']+)$/(caller)[$[]."'".$1/e;

X       local($_);

X               &main'ftw($_,"root_helper");
X       }
X}
X
Xsub root_helper {



X       local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,

X       if (($ino == 2) && ($dev > 0) && ($dev < 16384)) {

X       }
X       do $fn("$file"); # don't pass $file as an lvalue
X}
X
X1;
END_OF_FILE
if test 1684 -ne `wc -c <'ftw.pl'`; then
    echo shar: \"'ftw.pl'\" unpacked with wrong size!
fi
# end of 'ftw.pl'
fi
echo shar: End of shell archive.
exit 0
--
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |

\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/



Tue, 08 Mar 1994 06:31:24 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Recursing a directory tree

2. Need a program to compare directory trees

3. Traversal of a directory tree with housekeeping per directory

4. Seeking Tree::Base or other binary tree module to avoid reinventing the wheel

5. RFC Pod::Tree - tree-based POD parser

6. Needed: Script for file tree delete and file tree move

7. convert a perl tree into a C tree

8. Is there a Tree widget in perlTk that resembles the Tree widget in Tk

9. comparison of directory-trees with Perl 5

10. Output dynamic interactive graphical directory tree from perl to html

11. Directory tree

12. tool to pretty print directory trees?

 

 
Powered by phpBB® Forum Software