Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-06-13
9
Medium Priority
?
854 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1500 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

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

597 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