1833053 Members
2168 Online
110049 Solutions
New Discussion

Perl script

 
SOLVED
Go to solution
Stanimir
Trusted Contributor

Perl script

Could anybody send me a Perl-script for
encounting the sum of all files in
a directory /including files in subdirectories/, in bytes?
High points-level!!
11 REPLIES 11
John Poff
Honored Contributor

Re: Perl script

Hi,

Yes, but how about:

du -sk

JP
Kent Ostby
Honored Contributor

Re: Perl script

How about awk ?

find . -depth -exec ll {} \; | grep -v -e total -e drwx | awk '{datotal
+=$5;}END {print datotal}'

Or if you want to break it into two nicer steps:

find . -depth -exec ll {} \; | grep -v -e total -e drwx > useme

awk '{datotal+=$5;}END {print datotal}' < useme
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Umapathy S
Honored Contributor

Re: Perl script

JS,
I think du -s will do. du -sk will give in 1024 byte blocks.
Do you really need a perl script for this.

HTH,
Umapathy

Arise Awake and Stop NOT till the goal is Reached!
Jean-Luc Oudart
Honored Contributor

Re: Perl script

run a find + ll command (e.g. find . | xargs -l20 ll ) and pipe into the following script :

#!/bin/sh

# llsum_size : sum file size for ll result (file or stdin)
#
# usage : llsumsize [ filename ]
# filename : output of the 'll' command

awk '
BEGIN{ total=0; countfile=0; }
{
if(substr($1,1,1)=="-") {
total+=$5;
countfile++;
}
}
END{ totalmb= total / (1024 * 1024 );
printf("llsumsize : nbfiles counted = %d\n",countfile);
printf("llsumsize : total (bytes) = %12d\n",total);
printf("llsumsize : total (Mbytes) = %12.2f\n",totalmb);
} ' $1

Rgds,
JL
fiat lux
Brian Bergstrand
Honored Contributor

Re: Perl script

I know you want Perl, but as John already mentioned df already does this.

For bytes though, you'd want. du -ks will give the output in kilobytes. du -s will output the number of 512 byte blocks.

cd $MYDIR
echo $(( $(du -s . | cut -f1) * 512 ))

HTH.
Stanimir
Trusted Contributor

Re: Perl script

Thanks,Gays!!!
I know HPUX-command "du". :))
But I want exactly Perl-script !!!
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Perl script

This is a very easy script in Perl but it does you no favor to post it. The quest is the learning of Perl itself. If you do a man of File::Find, you should really have everything you need to get started.
If it ain't broke, I can fix that.
Sergejs Svitnevs
Honored Contributor

Re: Perl script

:)
#!/usr/bin/perl -w
system("du -sk");

Regards,
Sergejs
Sergejs Svitnevs
Honored Contributor

Re: Perl script

version nr. 2

#!/usr/local/bin/perl -w
my $path="/home/";
print "\n\nThe sum of all files in a directory '$path' (including files in subdirectories) is ", &Sizes ($path), " bytes \n";
sub Sizes {
my $dir = shift;
my $size = 0;
my $entry;
my @subDirs;
chdir $dir or die " chdir failed ($!)\n";
opendir DIR, "." or die " opendir failed ($!) \n";
while (defined ($entry = readdir DIR)) {
if (-f $entry) {
$size += -s _;
}
elsif (-d _ and $entry ne '.' and $entry ne '..') {
$size += -s _;
push @subDirs, $entry;
}
}
closedir DIR or die " closedir failed ($!)\n";
foreach $dir (@subDirs) {
$size += &Sizes ($dir);
}
chdir '..' or die " chdir .. failed ($!)\n";
return $size;
}

Regards,
Sergejs
Brian Bergstrand
Honored Contributor

Re: Perl script

use File::Find;

my $base = $ARGV[0] || "./";
my $total = 0;

sub addsize {
$total += -s;
}
find(\&addsize, $base);

print "$total\n";
H.Merijn Brand (procura
Honored Contributor

Re: Perl script

# perl -MFile::Find -le'find(sub{-f and$s+=-s},".");END{print$s}'

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn