1825730 Members
2630 Online
109687 Solutions
New Discussion

Re: size calc

 
shruti_1
Advisor

size calc

I have hundred of compressed files in one directoy. If i need to calculate the size of files ending with .Z how can i do so.
5 REPLIES 5
Ninad_1
Honored Contributor

Re: size calc

You can do

ls -l *.Z | awk '{tot+=$5} END {print "Total size =",tot/1024,"KB"}'

Regards,
Ninad
Steven E. Protter
Exalted Contributor

Re: size calc

Shalom shruti,

The bearer of bad news, me will tell there is no way to accurately predict how the files will compress. That depends on how compressible the data is in the files.

Any attempt to predict accurately the size of the resultant file will fail.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Kenan Erdey
Honored Contributor

Re: size calc

without awk:

top=0
for f in *.Z
do
size=`(ls -l $f | tr -s ' ' | cut -f5 -d' ')`
top=`expr $top + $size`
done
echo "total bytes of compressed files=" $top
Computers have lots of memory but no imagination
Peter Nikitka
Honored Contributor

Re: size calc

Hi,

I think you want to know wether the filesystem is able to hold all the files after uncompressing:
You won't be able to do this without really uncompressing each of them.

You can do an uncompress of one file and permanently check, if your freespace is big enough to hold the next.
I suggest something like that in ksh:


[ -d dir4flatfiles ] || mkdir dir4flatfiles
typeset -i free sz hog=100000

# you can set the factor statically or compute one dynamically as max(uncompr_size/compr_size)
for z in *.Z
do
sz=$(ls -s $z | awk -v factor=1.5 '{print $1*2*factor}')
free=$(bdf dir4flatfiles | awk 'NR>1 && NF>3 {print $(NF-2)}')
if [ free -gt sz+hog ]
then uncompress <$z >dir4flatfiles/${z%.Z}
else
print -u2 insufficient space for uncompress: $free
break
fi
done

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
spex
Honored Contributor

Re: size calc

You can zcat the *.Z files, and then count bytes:

#!/usr/bin/sh
# ./zcatsz.sh
for f in *.Z
do
echo $f $(zcat $f | wc -c) bytes
done

If you want the total size, pipe the script through awk:

./zcatsz.sh | awk '{t+=$2} END {print t "bytes"}'

PCS