Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to figure out the diskspace used? 6

Status
Not open for further replies.

rock31

Programmer
Jul 18, 2004
38
US
In our unix system, I was assigned a chunk of diskspace.
How could know how much I used, and how much remain?
Thanks
 
df -k (or i prefer df -h on linux) will show the system's file systems and how much space is used/assigned. If however you want to see how much space is used in a particular directory (not just a file system), you'll want to use some varient of the du command. Depending on your flavor of unix you might want to use different options. e.g. again not all of them support the -h flag which shows it in more human-readable output of M, G, K.. try doing a man on du and df and you should be able to figure it out..

[root@profserv cgi-bin]# du -sh /home/*
1.2M /home/adiwakar
492K /home/dferrant
1.9M /home/gwichman
16K /home/lost+found
740K /home/mprimici
84K /home/nagios
24K /home/rodeste
20K /home/smap
[root@profserv cgi-bin]# du -sh /home
4.4M /home

 
du to find out the diskspace you used each filewise.
df -k /dir to find out the disk free in that dir


cheers,
mpramods
 
Hi, Yrrk, and mpramods,

thank you very much.
 
Check out this exerpt from the book "Wicked Cool Shell Scripts". Just copy this to create a new script and run it to see available space on each filesystem.
Code:
#!/bin/sh

# diskspace - summarize available disk space and present in a logical
#    and readable fashion

tempfile="/tmp/available.$$"

trap "rm -f $tempfile" EXIT

cat << 'EOF' > $tempfile
    { sum += $4 }
END { mb = sum / 1024
      gb = mb / 1024
      printf "%.0f MB (%.2fGB) of available disk space\n", mb, gb
    }
EOF

df -k | awk -f $tempfile

exit 0
 
This will give you max to min
du -k | sort -nr | more



Cheers,
71
 
#!/bin/ksh
MAILTO="user@host.com"
echo "df -k output for `date` `uname -a`" > /tmp/df.out
case `uname -s` in
SunOS | Linux | OSF1 | UNIX_SV | FreeBSD )
if [ -s "/etc/redhat-release" ] ; then cat /etc/redhat-release >> /tmp/df.out ; fi
set -- `df -k | sed -e 's/%//g' | awk '/^\/dev/ { printf("%s %s\n", $5, $6) }'`
;;
HP-UX )
set -- `bdf | sed -e 's/%//g' | awk '/^\/dev/ { printf("%s %s\n", $5, $6) }'`
AIX )
set -- `df -k | sed -e 's/%//g' | awk '/^\/dev/ { printf("%s %s\n", $4, $7) }'`
;;
esac

while [ $# -ge 2 ] ; do
CAPACITY=$1
FS=$2
shift 2

if [ $CAPACITY -gt 90 ] ; then
echo "$FS is at $CAPACITY %" >> /tmp/df.out
fi
done
if [ `wc -l /tmp/df.out | awk '{ print $1 }'` -ge 2 ] ; then
cat /tmp/df.out | mail -s "`date`" $MAILTO
fi
 
I personnally use this awk program:
#!/usr/bin/awk -f
# cmd/df.awk Disk Free
BEGIN {
cmd="sync;LANG=C df -tk";fmt="%-28s %-23s%9s %9s %7s\n"
printf fmt,"MountDir","FileSystem","Total(Mb)","Free","Occup."
trt=sprintf(fmt,"----------","----------","---------","---------","------")
printf trt
fmt="%-28s %-23s%#9.2f %#9.2f %6.2f%%\n"
while((cmd|getline)==1){
if(substr($1,1,1)=="/"){
mountDir=$1
mountDev=substr($2,1,1)=="(" ? substr($2,2) : $2
sub(/[) \t]*:$/,"",mountDev)
nbFree=(substr($3,1,1)==")" ? $4 : $3)/1000
} else if($0~/otal/){
nbTot=($1~/otal/ ? $2 : $1)/1000
if(nbTot){
printf fmt,mountDir,mountDev,nbTot,nbFree,100*(nbTot-nbFree)/nbTot
tTot+=nbTot;tFree+=nbFree;tNb++
}}
}; close(cmd)
exit
}
END {
if(tTot && tNb>1){
printf trt""fmt,"Summary","All",tTot,tFree,100*(tTot-tFree)/tTot
}}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top