in reply to Get disk usage without modules
as a starting point, you might want to take a look at the following very basic subroutine which recursively traverses the subdirectory tree:
Many things would need to be added: management of symbolic links in accordance with your needs, adding accumulators for cumulative size of directories, etc.sub traverse_dir { my $dir = shift; my @entries = glob "$dir/*"; for my $entry (@entries) { if (-f $entry) { print "$entry:", -s $entry, "\n"; } elsif (-d $entry) { traverse_dir($entry); } else # perhaps do something for entries which are neither files + nor subdirectories, if that sort of thing exists } } } traverse_dir ".";
Also note that I have used the -d, -f and -s file test operators for simplicity, but in a real program, I would probably use the stat (and possibly lstat) built-in function instead. Also, you might want to replace the glob function call by a opendir and readdir combination, together with appropriate calls to grep to get rid of entries such as . and .. (which are skipped by glob).
Update: see To glob or not to glob for a discussion of glob and some of its advantages and disadvantages.
|
|---|