Computing.Net > Forums > Programming > Perl File Statistics

Perl File Statistics

Reply to Message Icon

Original Message
Name: drsmartman
Date: September 10, 2004 at 09:48:18 Pacific
Subject: Perl File Statistics
OS: RH AS 3.0
CPU/Ram: P3x4
Comment:

Hi all,

Writing some Perl code to extract information about a file.
Specifically, I want to find an md5 checksum, the file size
in bytes, the mime string and the mime human readable.
Currently, I do this using shell executions (listed below)
which works fine except that the size is rounded to the
nearest KB (or block...all my readings are the same), but
was wondering if there was a native way in Perl to do this
(quicker, easier). I'm sure there is, but I've had a difficult
time finding anything online or in my books. It seems
surprisingly simple too. Can anyone help?

How I am currently grabbing the information....throgh
Linux utilities...

md5 = `md5sum <file_name>`
size = `du -b <file_name>'
mime (human readable) = `file -b <file_name>`
mime (string) = `file -ib <file_name>`

Sample of results...

md5 = 9dkdnasksdjfkjalfjlajljf;ja; (whatever..a 32
characater hash)
size = 57382 (in bytes)
mime (human readable) = JPEG image data, JFIF standard
1.02, resolution (DPI), 72 x 72
mime (string) = image/jpeg


Many thanks for your help!!!!!!!


Report Offensive Message For Removal


Response Number 1
Name: Wolfbone
Date: September 10, 2004 at 10:20:47 Pacific
Reply: (edit)

Since perl wraps a lot of the std C library functions I know there is 'stat' for the filesize (man perlfunc) and if perl doesn't do md5 natively then there's bound to be a module at CPAN, [just looked - there is] But the mime stuff is almost certainly not going to be native - look for a CPAN module or just console yourself with the fact that using tool chaining is the *nix way of solving problems efficiently anyway :-)


Report Offensive Follow Up For Removal

Response Number 2
Name: drsmartman
Date: September 11, 2004 at 09:45:17 Pacific
Reply: (edit)

Thanks Wolfbone...the *nix tools work fine with the exception of the filesize. Above, I use 'du -b <filename>' but get a rounded out result. I need something more exact. So, I suppose the follow-up questions is, what is a *nix way of getting the 'exact' file size.

thanks for your comments. perhaps my way of doing things wasn't that bad in the first place.

Have a nice weekend!


Report Offensive Follow Up For Removal

Response Number 3
Name: Wolfbone
Date: September 11, 2004 at 10:31:30 Pacific
Reply: (edit)

Did you look up "stat" in the perlfunc manual page? It does just what the C library 'stat' does and one of the fields it returns is the filesize in bytes:

$bytes = (stat($filename))[7];

Other *nix commands like 'stat' and 'ls' will get you the filesize in bytes too but perl can do it without external help.


Report Offensive Follow Up For Removal

Response Number 4
Name: drsmartman
Date: September 11, 2004 at 20:58:00 Pacific
Reply: (edit)

Yeah, the stat command or the Perl one should do the trick! Thanks!


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Perl File Statistics

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge