25.8 Finding File Types
Many different kinds of files live on the typical UNIX system: database files, executable files, regular text files, files for fancy editors like Interleaf, tar files, mail messages, directories, font files, and so on.
You often want to check to make sure you have the right "kind" of file
before doing something. For example, you'd like to read the file
. But before typing
The file utility tells you what sort of file something is.  It's fairly self-explanatory:
file is actually quite clever [though it isn't always correct - some versions are better than others - JP ]. It doesn't just tell you if something's binary or text; it looks at the beginning of the file and tries to figure out what it's doing. So, for example, you see that file 2650 is an nroff (43.13 ) file and foo.sh is a shell script. It isn't quite clever enough to figure out that 0001,v is an RCS (20.14 ) archive, but it does know that it's a plain ASCII (51.3 ) text file.
System V and SunOS let you customize the file command so that it will recognize additional file types. The file /etc/magic tells file how to recognize different kinds of files. It's capable of a lot (and should be capable of even more), but we'll satisfy ourselves with an introductory explanation. Our goal will be to teach file to recognize RCS archives.
/etc/magic has four fields:
So, we know that RCS archives begin with the word
0 string head RCS archive
This says, "The file is an RCS archive if you find the string
As I said, the tests can be much more complicated, particularly if you're working with binary files. To recognize simple text files, this is all you need to know.