Solved

I want to write a ksh (korn shell) script to show disk usage per user.

Posted on 2006-06-13
9
828 Views
Last Modified: 2013-12-26
I want to write a ksh (korn shell) script to show disk usage per user. The script should take in a single command-line argument--the directory name, and display the usage in MB per user.

I have managed to get the files listings in the directory and then check who the owner of the file was. But, I am unable to associate the space taken up by the file to it's particular owner. If I could find a way to do that, I would simply add the space of the files of similar owners. Please let me know, if I'm thinking wrong and please suggest on how to proceed further. Associateive arrays might help but I dont know that they exist in ksh or not. If yes, then how do I create one and set values to it ??

NOTE: Associative arrays are different than the normal arrays. Please dont give me a way to declare a simple array. I tried typeset to create an associative array, but it dosen't work !!
0
Comment
Question by:Shweta_Singh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 16894941

You may need to use 'awk' to do it.
0
 

Author Comment

by:Shweta_Singh
ID: 16894977
Could you please write a sample code to show me what you mean ?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 16895788
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 16895827

PS: What have you got coded until now?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 16895938
0
 

Author Comment

by:Shweta_Singh
ID: 16896842
okay, my code as for now looks like this :


#!/usr/bin/ksh

#CREATE AN ARRAY AND STORE O/P OF du IN IT
set -A users `du -ak $1`

i=0
while [ $i -lt ${#users[*]} ]  # GO THRU THE ARRAY
do
    if [ [`test -f $users[$i]`] ]; then  #CHECK IF IT'S A FILE.

print ${users[$i]}

        #-- Get the values
        #diskspace=$users[$i]
        #entry_path=$users[$i+1]


        # GET THE OWNER NAME.
        owner=[`ls -la $entry_path | awk {'print $3'}`]

        #STUFF IT IN THE ASSOCIATIVE ARRAY
        typeset -A usage  # CREATING THE ARRAY
        #  usage["$owner"]=$diskspace   #OWNER REFERS TO THE SPACE HE TAKES UP.
        usage[ $owner ] = usage[ $owner ] + $diskspace  #OWNER REFERS TO THE SPACE HE TAKES UP.

#        print ${users[$i]}
    fi
    i=$i+2
done

0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 total points
ID: 16897095

Instead of using 'du' try something more simple like:

#!/bin/ksh
find $1 -ls|\
awk '{o[$5]=o[$5]+$7;}
END {for (i in o) print i " uses " o[i] " bytes.";}'
0
 

Author Comment

by:Shweta_Singh
ID: 16943246
Okay,
This is where I have reached uptil yet .

All you need to know is that I made an array and that I need to access it's variables which i'm unable to .
Pls help.


#!/usr/bin/ksh
#CREATE AN ARRAY TO STORE OWNERS TO COMPARE
set -A owners `ls -lR "${1}" 2>/dev/null | sed 1d | awk '{print $3}' | sort | uniq`

#LOOP OVER THE ARRAY TO GET THE OWNERS ONE BY ONE TO COMPARE
i=0
while [ $i -lt ${#owners[*]} ]
do
        `ls -lsR "${1}" | awk '/$owners[$i]/ {disk_usage += $6} END {print $owners[$i], " : ", 1*disk_usage, " MB"}'`

        ((i=i+1))
done
0
 

Author Comment

by:Shweta_Singh
ID: 16953482
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
dog bark java program 15 130
zeroMAx challenge 20 134
Sed question 2 139
ERP and the other system process verification 5 22
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question