"file" is also a
Unix command for determining the type of a file. Its output will almost always contain the word "
text" for a text file which can safely be viewed with "
more" or edited, and "
data" or "
executable" for a binary file.
file carries out three tests to determine a file's type. First it checks for empty files and for special files such as symbolic links. Then it consults the /etc/magic file, which has a list of "magic numbers" which appear near the beginning of binary files and the types of binary file they corespond to. Finally, if the file is an ASCII file, it attempts to guess its language by looking for keywords near the beginning of the file.
file does not always get the answer right - The Unix-Haters Handbook gives an example of the results obtained by typing "file *" in a fairly large directory. It seems to have particular difficulty in recognising Fortran, apparently because it only thinks a file is Fortran if it starts with a comment. Since my Fortran code tends to have "program name" before the comments, it is never recognised.