Solved

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

Posted on 2006-06-13
9
777 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
  • 5
  • 4
9 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
Comment Utility

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

Author Comment

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

Expert Comment

by:MikeOM_DBA
Comment Utility
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
Comment Utility

PS: What have you got coded until now?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 29

Expert Comment

by:MikeOM_DBA
Comment Utility
0
 

Author Comment

by:Shweta_Singh
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 149
countEvens challenge 2 56
NotAlone Challenge 20 70
Annoying "thing" blocks my view 4 49
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…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now