Category: File utilities; misc. stuff
Author/Contact Info Max Maischein aka Corion
Description:

This is the little brother of my ultimate file identifying script (which I'll never finish I fear) - it only identifies four different file types, but it can be easily expanded to recognize more file types. As the recognition is only RE-based, you'll have to be a bit ingenious to add some not-so-obvious types, and if you get conflicts between file types, the method of working through the hash keys will also fail.

On the other side, for the given file types, this works and that's all what is needed at the moment :).

Warning: This script renames files. If you care about your data, you test it with recoverable data first, before trusting it with important stuff.

#!/usr/bin/perl -w#!/usr/bin/perl -w
use strict;

# Simple script to identify file types and rename stuff accordingly
# Also my first script to use strict; ;D

my $bufferlength = 10;
my %signatures = (
  "zip" => "PK\x03\x04",
  "rar" => "Rar!",
  "mp3" => "\xFF[\xF0-\xFF]",
  "gz" => "\x1f\x8b\x08",
  "tar" => "\x66\x69\x78\x6e",
);

while (my $filename = shift) {
  open( FILE, "< $filename" ) or die "Can't open $filename : $!\n";
  binmode FILE; # just to be on the safe side
  my $read = read( FILE, my $buffer, $bufferlength ) or die "Can't rea
+d from $filename : $!\n";
  close FILE;
  # if the file was too small, skip it
  next if $read != $bufferlength;

  foreach (keys %signatures) {
    if ($buffer =~ /^$signatures{$_}/) {
      if ($filename !~ /$_$/i) {                                      
+                                  
        my $newname = $filename . "." . $_;
        print "$filename -> $newname\n";
        rename( $filename, $newname ) or die "Can't rename $filename t
+o $newname : $!\n"
      };
      last;
    };                                                                
+                            
  };
};
Replies are listed 'Best First'.
RE: File identifier
by slayven (Pilgrim) on Jun 06, 2000 at 21:20 UTC
    I'm just curious, why don't you use file(1) ?

    # file <filename>

      I do most of my stuff under NT, and there is no file(1) available ;) - also, why delve into bash programming when I can do it in Perl ...

        Useful Script.  
        How did you know to use:
        "\xFF\xF0-\xFF"
        for identifying MP3 files?
        
        I have tried unpack and sprintf, but I'm guessing I'm using those funcions wrong.