Solved

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

Posted on 2006-06-13
9
822 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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