1748158 Members
4304 Online
108758 Solutions
New Discussion юеВ

sub-directory size

 
SOLVED
Go to solution
Shivkumar
Super Advisor

sub-directory size

Dear Sirs,

What is command or script to find sub-directory or directory size in MegaByte (MB) or GB ?

Thanks in advance.
Shiv
7 REPLIES 7
David Child_1
Honored Contributor

Re: sub-directory size

$ du -sk subdir

This will give you the size in Kb. Multiply this by 1024 for Mb.

David
Rick Garland
Honored Contributor

Re: sub-directory size

The 'du' command has these options.

Patrick Wallek
Honored Contributor
Solution

Re: sub-directory size

Umm....Actually you would divide the output of 'du -sk' by 1024 to get the size in MB. Then divide again by 1024 to get GB.

If you multiplied the output by 1024, you'd get the size in BYTES.

Size in KB
# du -ks /somedir
1234567890 somedir

Size in MB
# echo 1234567890/1024|bc -l
1205632.70507812500000000000

Size in GB
# echo 1205632.705078125/1024|bc -l
1177.37568855285644531250
Ganesha Sridhara
Honored Contributor

Re: sub-directory size

Hello Shiv,

Size in KB
echo `du -ks | awk '{print $1}'`

Size in MB
echo `du -ks | awk '{print $1}'`/1024|bc -l

Size in GB
echo `du -ks | awk '{print $1}'`/1024/1024|bc -l

Regards
Ganesha Sridhara
Joel Girot
Trusted Contributor

Re: sub-directory size

Hi Shiv

See this attached script, found on this forum. Look like that :

/# dumegs *
bin 0 Kb
cdrom 0 Kb
dev 104 Kb
etc 48,712 Mb
gv0 118,830 Mb
gv1 3,461 Gb
home 592 Kb
lib 0 Kb
log 1 Kb
lost+found 0 Kb
net 0 Kb
opt 1,709 Gb
root 4,608 Mb
sbin 36,728 Mb
stand 40,968 Mb
tmp 27 Kb
tmp_mnt 0 Kb
usr 1,174 Gb
var 728,632 Mb

Joel
Mahesh Kumar Malik
Honored Contributor

Re: sub-directory size

Hi David

We have to devide by 1024 to get size in MB

Regards
Mahesh
Joel Girot
Trusted Contributor

Re: sub-directory size

Sorry, attachment did not work, here his the script :

#!/usr/bin/ksh
# du in Kbytes, Mbytes, Gbytes
# ============================
# usage dumegs [-p directory] [directories]
# option : -p directory : percent of this parent directory
# other parameter : directories to display

# fonction du -s
du_it()
{
typeset -R10 vsize
typeset -R3 vpc

bytes=`du -s $1 | cut -f1`

if (( ${#1} < 8 )) ; then
ztab="\t\t\t"
elif (( ${#1} < 16 )) ; then
ztab="\t\t"
else
ztab="\t"
fi
if [ $bytes -le 999999 ] ; then
bytes=`expr $bytes \* 512`
kbytes=`expr $bytes \/ 1024`
else
kbytes=`expr $bytes \/ 2`
fi

if [ $kbytes -gt 999 ] ; then
mbytes=`expr $kbytes \/ 1024`
xbytes=`expr $mbytes \* 1024`
dbytes=`expr $kbytes - $xbytes`
if [ $dbytes -le 10 ] ; then
dbytes="00$dbytes"
fi
if [ $dbytes -le 100 ] ; then
dbytes="0$dbytes"
fi
if [ $mbytes -gt 999 ] ; then
gbytes=`expr $mbytes \/ 1024`
xbytes=`expr $gbytes \* 1024`
dbytes=`expr $mbytes - $xbytes`
if [ $dbytes -le 10 ] ; then
dbytes="00$dbytes"
fi
if [ $dbytes -le 100 ] ; then
dbytes="0$dbytes"
fi
vsize=$gbytes,$dbytes
unit=" Gb"
else
vsize=$mbytes,$dbytes
unit=" Mb"
fi
else
vsize=$kbytes
unit=" Kb"
fi
if [ -n "$optp" ] ; then
if [ -n "$tkb" ] ; then
vpc=$(expr $kbytes \* 100 \/ $tkb)
pc=" $vpc %"
else
pc=" (parent directory)"
fi
fi
echo "$1$ztab$vsize$unit$pc"
}

# main dumegs
tkb=
optp=
optparg=
while getopts :p: OPT "$@"
do
case $OPT in
p) optp=1 ; optparg=$OPTARG ;;
\?) echo "usage dumegs [-p directory] [directories]" ; exit 1 ;;
esac
done

if [ -n "$optp" ] ; then
if [ -z "$optparg" ] ; then
echo "option -p : missing directory parameter"
exit 2
fi
du_it $optparg
tkb=$kbytes
shift 2
fi

if [ $# -gt 0 ] ; then
for dirs in $*
do
if [ -d "$dirs" ] ; then
du_it $dirs
elif [ -f "$dirs" ] ; then
du_it $dirs
fi
done
else
du_it $PWD
fi
$