use File::Find;
# Get size of entry (file or directory) in bytes.
sub entry_size {
if (-f $_[0]) {
-s $_[0];
} elsif (-d $_[0]) {
my $size = 0;
finddepth( sub { $size += -s if -f $_ }, $_[0] );
$size;
}
}
Notes:
- File::Find module - Traverse a directory tree. Usage:
finddepth(\&wanted, @directories);
finddepth(\%options, @directories); - File tests and their meanings
File test Meaning -r File or directory is readable by this (effective) user or group -w File or directory is writable by this (effective) user or group -x File or directory is executable by this (effective) user or group -o File or directory is owned by this (effective) user -R File or directory is readable by this real user or group -W File or directory is writable by this real user or group -X File or directory is executable by this real user or group -O File or directory is owned by this real user -e File or directory name exists -z File exists and has zero size (always false for directories) -s File or directory exists and has nonzero size (the value is the size in bytes) -f Entry is a plain file -d Entry is a directory -l Entry is a symbolic link -S Entry is a socket -p Entry is a named pipe (a “fifo”) -b Entry is a block-special file (like a mountable disk) -c Entry is a character-special file (like an I/O device) -u File or directory is setuid -g File or directory is setgid -k File or directory has the sticky bit set -t The filehandle is a TTY (as reported by the isatty() system function;
filenames can’t be tested by this test)-T File looks like a “text” file -B File looks like a “binary” file -M Modification age (measured in days) -A Access age (measured in days) -C Inode-modification age (measured in days)
- @_ is the list of parameters to a sub. So if you write a sub, you refer to the first parameter in it as $_[0], the second parameter as $_[1] and so on. $_ is known as the "default input and pattern matching space."
沒有留言:
張貼留言