Operating System - HP-UX
1834463 Members
3138 Online
110067 Solutions
New Discussion

Re: Writing a Script: du -k

 
sai.prashant
Occasional Advisor

Writing a Script: du -k

Hello,
Please helpme out in writing a script for the below scenario:

I have /Home directory under which i have all the User's Home directory as shown below. I have to write a script which should generate an output showing User Home Directory, Disk Space Used in KB, % Disk Space Used in descending order.

bdf command ouput for /Home:
/vol/vol1/home/tatc-in 73400320 63090040 10310280 86% /tmp_mnt/home/

Directories under /Home:
cmuluktl d03013 d03019 mappukut sprashan
DEL1.KAL d03001 d03014 d03020 mprakash sramanat
FINAL d03004 d03015 d03021 report_disk twr16
USftp d03005 d03016 d03023 sdhondi twr17
anandam d03009 d03017 indiaftp skondapa vbojja
apemmara d03010 d03018 kkomati smasabat

Thanks,
Sai
8 REPLIES 8
Bharat Katkar
Honored Contributor

Re: Writing a Script: du -k

Hi Sai,
# du -k | sort -k 1,1
This works for me.

for e.g.

# pwd
/home
# du -k
0 ./lost+found
8 ./one
8 ./two
16 .
# du -k | sort -k 1,1
0 ./lost+found
16 .
8 ./one
8 ./two
# ls -al
total 16
drwxr-xr-x 5 root root 96 Aug 11 12:54 .
drwxr-xr-x 15 root root 8192 Aug 10 19:02 ..
drwxr-xr-x 2 root root 96 Aug 9 18:12 lost+found
drwxrwxrwx 2 root sys 96 Aug 11 12:54 one
drwxrwxrwx 2 root sys 96 Aug 11 12:54 two
#

Regards,
You need to know a lot to actually know how little you know
Jose Mosquera
Honored Contributor

Re: Writing a Script: du -k

Hi,


Try this script:

#!/sbin/sh
OUTPUT=" Kb Directory"
DIRS=`ll|grep ^d|awk '{ print $9}'`
for DIR in $DIRS
do
OUTPUT="$OUTPUT\n`du -ks $DIR`"
done
echo "$OUTPUT"|sort -n

Rgds,
Bharat Katkar
Honored Contributor

Re: Writing a Script: du -k

Sai,
sorry for that.. little confusion...
This should work.
# du -k | sort -k 1n,1

Regards,
You need to know a lot to actually know how little you know
Michael Tully
Honored Contributor

Re: Writing a Script: du -k

There are a couple of perl offerings in the old "system admin" scripts thread.

See ernesto's here:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=51050

and Ben Sachs
http://forums1.itrc.hp.com/service/forums/parseCurl.do?CURL=%2Fcm%2FQuestionAnswer%2F1%2C%2C0x836cc1c4ceddd61190050090279cd0f9%2C00.html&admit=716493758+1092210687681+28353475

There is a gold mone of scripts in the 3 threads.
Anyone for a Mutiny ?
Michael Campbell
Trusted Contributor

Re: Writing a Script: du -k

Sai

Here's a handy one:

rm /tmp/du_dir_list 2>/dev/null
rm /tmp/du_dir_list2 2>/dev/null
echo " "
echo "FILES:"
echo " "
ll | grep -v ^d |sort -k 5n

ll | grep ^d |cut -c59- > /tmp/du_dir_list
if [ -s /tmp/du_dir_list ]
then
echo " "
echo "DIRECTORIES (in Kb):"
echo " "
for dirname in `cat /tmp/du_dir_list`
do
du -sk $dirname | awk '{ printf("%8d %s\n",$1,$2) }' >>/tmp/du_dir_list2
done
sort /tmp/du_dir_list2
else
echo " "
echo "There are no Directories:"
echo " "
fi
rm /tmp/du_dir_list 2>/dev/null
rm /tmp/du_dir_list2 2>/dev/null
echo " "
bdf .
MarkSyder
Honored Contributor

Re: Writing a Script: du -k

To sort the directories in descending order:

du -k|sort -k1,1nr

The nr specifies numerical reverse (default is smallest first)

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Bharat Katkar
Honored Contributor

Re: Writing a Script: du -k

Muthukumar_5
Honored Contributor

Re: Writing a Script: du -k

You can do with the following script simply

#!/usr/bin/ksh

for user in `ls /home`; do

fssize=`bdf /home | grep -v "^Filesystem" | awk '{ print $2 }'`
homesize=`du -ks /home/$user | grep -v "^Filesystem" | awk '{ print $1 }'`

if [[ $homesize -ne 0 ]]
then
per=$(echo "$homesize $fssize" | awk '{ print $1/$2*100 }')
else
per=0
fi

home=`du -ks /home/$user | awk '{ print $2 }'`

echo "$home $homesize $fssize $per %"
done >> /tmp/form.log
echo "HOME DIRECTORY USAGE DISK SPAGE %DISK SPACE USAGE"
cat /tmp/form.log | sort -r -k 4
rm -f /tmp/form.log

## end ###

Output:
=======
# ksh /tmp/form.ksh
HOME DIRECTORY USAGE DISK SPAGE %DISK SPACE USAGE
/home/muthu 3152 32768 9.61914 %
/home/testusr 32 32768 0.0976562 %

Regards
Muthu

Easy to suggest when don't know about the problem!