Group and count files by type in Perl (using file magic numbers)

Perl File::Find - passing parameters to „wanted” callback - basics

Using object oriented libraries with File::Find module is not so obvious like it should be. But there is a way to do it in simple way - use closures in Perl. Now you probably think, „stop the bla bla talk, and show me code!”, so here it is:

How to determine file type ignoring extension?

Answer is simple - using magic numbers.

What is magic number

The term magic number is a constant numerical or text value used to identify a file format or protocol.

How connect file type with magic number in Perl?

There is variety of modules implementing this, for sample application presented below I have used File::Type module. Installation of this module on Ubuntu is extreme easy: apt-get install libfile-type-perl.

Tool for finding files of certain type using magic numbers

Here you can download source code of this program: findGroup.pl

How to use the tool?

$ perl findGroup.pl .
Directory . contains:
   17 application/octet-stream
   2 application/x-perl
   3 application/zip
   346 image/jpeg
$ perl findGroup.pl . perl
2 files of type application/x-perl (matching: perl) in .
   ./findGroup.pl
   ./test.pl

Additional references:

Comments

comments powered by Disqus