Operating System - HP-UX
1758643 Members
2279 Online
108874 Solutions
New Discussion юеВ

Scipt that lists the amount of space by user

 
SOLVED
Go to solution
Jay_122
Advisor

Scipt that lists the amount of space by user

Does anyone have a script that lists the amount of space used per user? Any help would be appreciated.

10 REPLIES 10
Peter Godron
Honored Contributor

Re: Scipt that lists the amount of space by user

Jay,
on all the mount points or one directory?
Peter Godron
Honored Contributor

Re: Scipt that lists the amount of space by user

Jay,
a FIRST attempt
#!/usr/bin/sh
user=$1
# Start at the root directory
cd /
# Find all entries owned by the user and extract the filesize
find . -user $user -exec ls -ld {} \; | awk -F' ' '{print $5}' > /tmp/$$
a=0
while read record
do
a=`expr $a + $record`
done < /tmp/$$
echo "User $user : $a bytes"
rm /tmp/$$

I'm sure perl has got a better way of adding up the bytes.
Regards
Jay_122
Advisor

Re: Scipt that lists the amount of space by user

Directory.
Hein van den Heuvel
Honored Contributor

Re: Scipt that lists the amount of space by user

Did someone mention perl? :-)

The following perl morcel with print the allocated space for each user in the current directory:

while (<*>) {
($u,$b)=(stat $_)[4,12];
$total{$u}+=$b;
}
foreach $u (sort keys %total) {
printf("%10s%6d%10d\n",
scalar getpwuid($u),$u,512*$total{$u});
}


cheers,
Hein.
Jay_122
Advisor

Re: Scipt that lists the amount of space by user

hein,
Does it include sub-directories in the current directory?
Hein van den Heuvel
Honored Contributor

Re: Scipt that lists the amount of space by user


Ah, no. You wrote 'directory', 'and all its's sub directories' :-).


I would enlist the help of the 'find' function:

use File::Find;
print "-".@ARGV."-\n";
sub add_blocks {($u,$b)=(stat $_)[4,12]; $total{"$u"}+=$b};
find (\&add_blocks, @ARGV);
foreach $u (sort keys %total) {
printf("%10s%6d%10d\n",
scalar getpwuid($u),$u,512*$total{$u});
}

Or... I might just go back to the find, or ls -lR command and deal with the output.

Hein,.

Hein van den Heuvel
Honored Contributor
Solution

Re: Scipt that lists the amount of space by user


Argh, I meant to write that you originally did not write "and it's subdirectory", just "directory".
And I posted a test version.
And I did not give usage instructions:

perl allocated_by_user /some_top_dir [/more_directories]

------- allocated_by_user -----
use File::Find;
sub add_blocks {($u,$b)=(stat $_)[4,12]; $total{"$u"}+=$b};
find (\&add_blocks, @ARGV);
foreach $u (sort keys %total) {
printf("%10s10d\n",
scalar getpwuid($u),512*$total{$u});
}

Bill Hassell
Honored Contributor

Re: Scipt that lists the amount of space by user

Or you might just turn on quotas for the mountpoints where the user has write privileges. Generally, /home /var and /tmp are (should be) the only mountpoints where users can create files. Quotas can be adjusted for each user and each mountpoint.


Bill Hassell, sysadmin
Gordon  Morrison_1
Regular Advisor

Re: Scipt that lists the amount of space by user

Presumably these are user's home directories you want to measure?

cd /home
du -sk *|sort -n > naughtyusers.txt


What does this button do?