l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
July 7: Social gathering
Next Installfest:
Latest News:
Jun. 14: June LUGOD meeting cancelled
Page last updated:
2004 Feb 10 13:49

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] Perl help requested
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] Perl help requested

--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:

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
vox-tech mailing list

LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Appahost Applications
For a significant contribution towards our projector, and a generous donation to allow us to continue meeting at the Davis Library.