in reply to file type

Update: For monks who are unfamiliar with the Unix file command, it is a utility that examines the contents of a file and reports the file type. It works on both binary and text files, using magic numbers and/or string tables to make its determination. Here's an example:
% file /usr/bin/perl /usr/bin/perl: ELF 32-bit LSB executable, Intel 80386, version 1, dyna +mically linked (uses shared libs), stripped

I tried running file on a few of my C source files, and got back 'C program text'. So file can figure out when an ASCII file consists of C code.

Looking at the manpage for file (on RedHat), I see:

If an argument appears to be an ASCII file, file attempts to guess its language. The language tests look for par­ ticular strings (cf names.h) that can appear anywhere in the first few blocks of a file. For example, the keyword .br indicates that the file is most likely a troff(1) input file, just as the keyword struct indicates a C pro­ gram. These tests are less reliable than the previous two groups, so they are performed last. The language test routines also test for some miscellany (such as tar(1) archives) and determine whether an unknown file should be labelled as `ascii text' or `data'.
Two observations: file guesses on the contents of an ASCII file by examining the first few blocks of the file, and the files you tested this on happen not to contain the strings that file is looking for.

I'm not sure what the best solution is. The file extensions will certainly be useful, as others have suggested.

Replies are listed 'Best First'.
Re: Re: file type
by Anonymous Monk on Feb 09, 2001 at 05:21 UTC
    Yes, I came across new things. As you suggested I unable to find any other betterway. On my unix system File::MMagic is not available. So I am opening each file and searching for either c or C++ type comments and deciding the language it belongs. I unable to go by file extention. Since I came across with various extentions like .c, .C, .cpp, .CPP, .cxx,.CXX,.pc, .PC etc. Thanks Ashok
      As best as I can tell, Perl's built-in grep command does not look inside an unopened file for you.

      But if that is what you want to do, why not use grep and the backtick operator? It is even possible that Grep may avoid scanning in a whole file if it knows that a pattern has failed halfway through.

      Even if grep doesn't exist on a Windows system you can always use the GNU port of grep which is available here.