#!/usr/bin/perl # Find number of directories, files, text files # and sum number of lines of text files from a given path # Coded by Trizen under the GPL. # Coded on 11 December 2011 # http://trizen.googlecode.com use strict; my $d; # number of dirs my $f; # number of files my $t; # number of text files my $l; # number of lines sub count_files { my $ref = shift @_; foreach my $dir (@$ref) { $dir = readlink $dir and chop $dir if -l $dir; # read link next unless opendir(my $dir_h, $dir); # open dir or next my @dirs; while (defined(my $file = readdir $dir_h)) { # Next only if dir is either '.' or '..' # if ($file eq '.' or $file eq '..') { # next; # } next if chr ord $file eq '.'; # next if dir starts with a dot '.' if (-d "$dir/$file") { ++$d; # counting dirs push @dirs, "$dir/$file"; } else { ++$f; # counting not dirs if (-f -T "$dir/$file") { # if file is really a file and plain text open my $fh, '<', "$dir/$file" or next; # ...open it 1 while (<$fh>); # go to last line $l += $.; # get last line number ($.) ++$t; # counting text files } } } closedir $dir_h; count_files(\@dirs); } [$f, $d, $l, $t]; # return sum of files, directories, lines and text files } foreach my $arg (@ARGV) { my @dir = -d $arg ? $arg : next; ($d, $f, $t, $l) = (0, 0, 0, 0); print "$arg Files\t: $$_[0] Dirs\t: $$_[1] Lines\t: $$_[2] (from $$_[3] files) " foreach count_files(\@dir); }