This is an old revision of the document!
Corning Community College
CSCS1730 UNIX/Linux Fundamentals
~~TOC~~
To become familiar with another useful utility, and to develop some basic debugging/diagnostic abilities.
The filetype of a file can be extremely important when determining what application is used to open it.
Most of the time a file is named correctly, for instance a file ending in .c can be assumed to be the source code of a C program, or .gz to be a gzipped file.
With the dircolors(1) utility colorizing specific files, it is further assuming that files which end in .mpg are really MPEG files and colors them accordingly, and the same for .zip files, etc.
In other operating systems, a file's extension determines what application is used to open the particular file. If a file that ends in .mp3 is really a .png file, the default MP3 player is going to have difficulties.
Sometimes files are not always named properly, either due to a web browser mangling an extension or for whatever reason. When a file is more than meets the eye, we must rely on the various tools available to use to determine what in fact it is.
In UNIX there is a nifty little utility called file that attempts to determine the actual type of a file by checking a series of properties. From the file(1) man page:
There are three sets of tests, performed in this order:
The first test that succeeds causes the file type to be printed.
A filesystem test checks to see if the file is non-ordinary (such as a socket, symbolic link, or other special file).
The magic number test is a check of files conforming to existing fixed formats, typically by examining the file at the binary level. If using a hex editor, you will find that .gz files should always start with the same sequence of hexadecimal values.
Finally, if the file is determined to be a simple ASCII file, it will attempt to analyze whether or not it conforms to some language (ie C source code vs. an HTML document).
Note that file is not always perfect, but for most cases will get the job done. Try checking files in your home directory or elsewhere on the system and see the results.
Refer to the file(1) manual page or your textbook for more information.