[alt.sources] Short 'n' dirty file space totaller

karl@ddsw1.UUCP (10/06/87)

Here's a real short, cheap-n-dirty disk usage tracker.  Pipe the output of
this program through sort(1) with the proper options, and you've got a nice
report of who's the biggest disk pig on your system.

Note that this is a CHEAP AND DIRTY way of doing the job, it's not
optimized, scrunched, or even reasonanbly conscientious about resource
consumption.  It also won't find files that are deliberately hidden (ie: not
in a user's $HOME).  It requires that the utility 'du' be on the system
somewhere in your PATH.  Lastly, it's unsupported, and has no man page.  But
what do you want for a thousand bytes of 'C' or thereabouts.

It does write errors to stderr, and output to standard output (stdout), so
at least you can separate errors (and warnings) from the useful part.

This code is hereby deemed public domain.  Do what you want with it.

/*	Make a disk usage report */
/*	Authored sometime in 1987 by Karl Denninger because we needed
	it to keep track of the disk pigs on our system			*/

#include	<stdio.h>
#include	<fcntl.h>
#include	<sys/types.h>
#include	<pwd.h>
#include	<errno.h>

extern	struct	passwd	*getpwent();

main()
{

	struct	passwd	*psw;
	FILE	*fid;
	char	tmp[80];

	while ((psw = getpwent()) != NULL) {
		if (psw->pw_uid >= 100) {		/* Ignore < 100 */
			if ((open(psw->pw_dir, O_CREAT|O_EXCL,000) < 0 ) &&
			    (errno = EISDIR)) {
				printf("%12s: ", psw->pw_name);
				fflush(stdout);
				strcpy(tmp, "cd ");
				strcat(tmp, psw->pw_dir);
				strcat(tmp, ";du|tail -1|cut -d. -f1");	/* How many blocks? */
				system(tmp);
				fflush(stdout);
			} else {
				unlink(psw->pw_dir);
				fprintf(stderr,"No directory for %s (%d)\n", psw->pw_name, psw->pw_uid);
			}
		} else fprintf(stderr,"Skipped user %s (%d)\n",psw->pw_name, psw->pw_uid);
	}
	fclose(fid);
	exit(0);
}

-- 

Karl Denninger				UUCP : ...ihnp4!ddsw1!karl
Macro Computer Solutions		Dial : +1 (312) 566-8909 (300-1200)
"Quality solutions at a fair price"	Voice: +1 (312) 566-8910 (24 hrs)

henry@utzoo.UUCP (Henry Spencer) (10/11/87)

> Here's a real short, cheap-n-dirty disk usage tracker.  [40 lines of C]

Um, did you consider:

	du -s `awk -F: '{print $6}' /etc/passwd` | sed 's;.*/;;'

(Three assumptions:  your du has the -s option [I've never heard of one that
doesn't], the home directory is the sixth field in /etc/passwd, and the home
directories have names corresponding to the users.  Failures in these
assumptions can generally be bypassed by making it a bit fancier.)

This works just as well.  (Subject to the flaw that's common to any du-based
disk accounting, to wit updating the accessed-time on all directories.)
In general, if you find yourself writing a C program whose main job is to
invoke other programs, you should probably try to do it as a shell program
first.
-- 
"Mir" means "peace", as in           |  Henry Spencer @ U of Toronto Zoology
"the war is over; we've won".        | {allegra,ihnp4,decvax,utai}!utzoo!henry

dave@lsuc.UUCP (10/12/87)

In article <8738@utzoo.UUCP> henry@utzoo.UUCP (Henry Spencer) writes:
>> Here's a real short, cheap-n-dirty disk usage tracker.  [40 lines of C]
>
>Um, did you consider:
>
>	du -s `awk -F: '{print $6}' /etc/passwd` | sed 's;.*/;;'

Even simpler, for most systems, will be something like
	du -s /u/*
(or /u?/* or whatever happens to match the directory/ies where
your users reside).

David Sherman
-- 
{ uunet!mnetor  pyramid!utai  decvax!utcsri  ihnp4!utzoo } !lsuc!dave
Pronounce it ell-ess-you-see, please...

chuck@amdahl.UUCP (10/14/87)

In article <2074@lsuc.UUCP> dave@lsuc.UUCP (David Sherman) writes:
>In article <8738@utzoo.UUCP> henry@utzoo.UUCP (Henry Spencer) writes:
>>> Here's a real short, cheap-n-dirty disk usage tracker.  [40 lines of C]
>>
>>Um, did you consider:
>>
>>	du -s `awk -F: '{print $6}' /etc/passwd` | sed 's;.*/;;'
>
>Even simpler, for most systems, will be something like
>	du -s /u/*
>(or /u?/* or whatever happens to match the directory/ies where
>your users reside).
>
>David Sherman
>{ uunet!mnetor  pyramid!utai  decvax!utcsri  ihnp4!utzoo } !lsuc!dave

Um...  Suppose my users have files scattered all over the filesystem
and not just in there home directories.  How do I find out who the
disk hogs are then?

(Fortunately, on our system, someone implemented a command to
do this sort of thing, and even documented the command line to use
to find out the above.  But what do mere mortals do on other
Unix systems?)

-- Cs