1833588 Members
4295 Online
110061 Solutions
New Discussion

Calculating file usage

 
SOLVED
Go to solution
Tony Walker
Frequent Advisor

Calculating file usage

Hi Guys,

I'm currently on a project to migrate an old K-class server to an NT box (I know - I wasn't impressed either). One of the filesystems is 117GB in size and I have been asked to get all files over 6months old, calculate how much space they all take up and then do it on a per user basis.

I have done a find /myfs -mtime +168 -exec ll {} \; > list.

What I wanted to do was add each of the size fields together ($5) which i attempted in a 'for' loop this has worked but tells me I can get rid of 88GB! This may be true but I want to check whether my figures are correct and also get suggestions on an easier/better way of doing this. Advice would also be appreciated for getting the amount each individual user is using.

Thanks in advance,

Ton
10 REPLIES 10
Chris Wilshaw
Honored Contributor

Re: Calculating file usage

You could try

TOTAL=`awk '{print (s+=$5/1073741824}' $FILENAME | tail -1

A bit shorter if nothing else.
(the 1073741824 is 1024*1024*1024)

Chris
Pete Randall
Outstanding Contributor

Re: Calculating file usage

Ton,

I ran a quick test and it came up with 5 when my manual calculations came up with 5.9 GB. I think I'd save the division for the end - it looks like it's rounding. In other words, eliminate the
(( i = (i / 1024) /1024)) line
and divide by 1024 again in the
GB=$(expr $TOTAL / 1024) line.

Pete

Pete
Jerome Baron
Respected Contributor

Re: Calculating file usage

Hi Ton,

Try find /myfs -type f -atime +168 -exec du -k {} \; > list

Regards,
Jerome
Chris Wilshaw
Honored Contributor

Re: Calculating file usage

That's the problem with using expr.

It only deals in integers.

expr 10 / 3
3
Jose Mosquera
Honored Contributor

Re: Calculating file usage

Hi,

Try this script:

TOTAL=0
SIZES=`find /myfs -mtime +168 -exec ll {} \;|awk '{ print $5 }'`
for SIZE in SIZES
do
TOTAL=`expr $TOTAL + $SIZE`
done
echo "Total Size: $TOTAL Bytes"

Rgds.
Tony Walker
Frequent Advisor

Re: Calculating file usage

Pete, I tried this approach originaly but after adding about 7 large numbers together it started producin negative numbers!? Thats why I did the Mb conversion before the addition. I'm assuming that it is working correctly and that my logic is sound? Good point with the exec du -k {}\; - I'll give that a go....

Any thoughts on calculating it per user? This is what I have so far but it seems to product differing results (e.g 5user in a row have the same space then 1 has 17GB!?)

Cheers,

Tony
harry d brown jr
Honored Contributor

Re: Calculating file usage

Ton,

Here's a VERY simple "perl" program:


#!/opt/perl/bin/perl
$SYMBOLS=" KMGTP";
$totarr="";
while (<>) {
tr/ / /s;
($tmp1,$tmp2,$username,$grpname,$filesize,$tmp3) = split(/ /);
if ($username ne "") {
$totarr{$username} += $filesize;
}
}
foreach $username (sort keys %totarr) {
$filesizes=$totarr{$username};
$spaceinuse=0;
$spacesymbol="";
for ($i=0;$i < 6;$i++) {
if ($filesizes >= (1024**$i)) {
$spaceinuse=$filesizes/(1024**$i);
$spacesymbol=substr($SYMBOLS,$i,1);
}
}
printf("%9s %15.3f %sB, bytes is %s\n",$username,$spaceinuse,$spacesymbol,$totar
r{$username});



To use it, do something like this:

perlprogram.pl < yourfilelist


live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor
Solution

Re: Calculating file usage

I attached a commented and indented version


live free or die
harry
Live Free or Die
Tony Walker
Frequent Advisor

Re: Calculating file usage

Harry,

Thanks for that - a good platform for me to start learning perl. I look forward to when I can call that VERY simple :) Thanks to everyone for your responses.

cheers,

Tony
Tony Walker
Frequent Advisor

Re: Calculating file usage

Harry,

With my limited Perl knowledge I set about editing your script so that it would just produce the total amount of space taken up by all the files. The result is 152GB which I know cannot be right! Could you point me in the right direction?

Thanks,

Tony