
# UNISRC_ID: @(#)duusers.sh	27.1	85/03/10  
# Sum and sort total disc usage by user for all users.

# Should be run by root or you may get errors (unable to read things).


	PATH=/bin:/usr/bin:/usr/contrib/bin

	temp=/tmp/du$$
	trap "rm -f $temp; trap '' 0; exit" 0 1 2 3


# Get list of home directories:

	dirs=`	users -l		|	# lines of user information.
		grep "/bin/[^ ]*sh"	|	# select those who can log in.
		sed -e "s:[^/]*::"	\
		    -e "s: .*::"		# strip up to "/" and past " ".
	     `

# Sum blocks and count files for each user, and format:

	for dir in $dirs
	do
	{	du -sr $dir			# line 1.
		find $dir -print | wc -l	# line 2.
	}	|
		awk '	(NR == 1) { split ($0, fields); }	# save.
			(NR == 2) { printf ("%6d %5d  %s\n", \
				    fields[1], $1, fields[2]);
			}
		' >> $temp
	done


# Sort, total, and print:

	sort -nr < $temp	|
	awk '	BEGIN {	print "blocks files  directory";
		}
		{	blocks	+= $1;
			files	+= $2;
			print;
		}
		END {	printf ("%6d %5d\n", blocks, files);
		}
	'
