[vox-tech] Perl help requested
Ken Herron
vox-tech@lists.lugod.org
Tue, 10 Feb 2004 11:59:38 -0800
--On Tuesday, February 10, 2004 09:56:36 -0800 Richard Crawford
<rscrawford@mossroot.com> wrote:
> I'm trying to write a Perl application that will go through a directory
> and give the amount of space used by each directory, including each
> subdirectory. So, for example, if myDirectory1 has three files
> totalling 150K and two subdirectories (mySubDirectoryA, with 20K of
> files, and mySubDirectoryB with 50K of files), I'd like to see output
> like this:
>
> DIRECTORY SIZE
> ======================
> myDirectory1 220K
You know, you're basically describing the "du" program. Assuming du
doesn't meet your needs as-is, your perl script could launch du with
appropriate arguments and massage the output as needed.
For an all-perl solution, I'd probably use File::Find to traverse the
directory structure of interest and track total sizes using a hash table
of directory paths. For each file you'll need two pieces of information:
1) its size, obtained by stat'ing it.
2) its pathname, which should be available through a File::Find variable.
You'll parse this into all of the ((sub-)sub-)directories containing the
file, in order to collate total sizes by directory.
You'll want to consider the following:
1) Are the sizes of the directories themselves part of the totals? Keep
in mind that if the directory tree were moved to a different filesystem,
the directories might a different amount of space.
2) What to do with hard links? If a file appears twice within the
directory tree, does it count once or twice?
3) What to do with symlinks? The symlink itself takes up a small amount
of space, and it refers to a file which might or might not be within the
directory tree.
There's no one right way to handle these; it just depends on what you're
doing with the totals.
--
"Grand Funk Railroad paved the way for Jefferson Airplane, which cleared
the way for Jefferson Starship. The stage was now set for the Alan Parsons
Project, which I believe was some sort of hovercraft." - Homer Simpson
Kenneth Herron Kherron@newsguy.com 916-366-7338