Main index

Introducing UNIX and Linux


Files

Overview
The UNIX directory hierarchy
Filesystems
Manipulating files
      Creating directories
      Creating files
      links
      'Dot' files
Protecting files
      Groups
      File access control
      Changing privileges
File contents
      Text files
      Comparing files
      Filtering files
      Non-text files
Printing files
File archives and file compression
Other relevant commands
Summary
Exercises

File contents

Given that you know a file exists, an obvious question is: 'What does it contain?' A simple answer to this question might be that it contains a sequence of bytes - but this would not be very helpful. We need to know what those bytes represent. We could start off by examining the filename; some UNIX files are required to have a particular suffix, and this information could indicate their contents. However, we should note that, for instance, if a file is to contain a C program, its suffix must be .c, but the converse does not hold. If we encounter a file called myfile.c then this does not mean that the file must contain a C program (although it would be perverse if in fact it did not).

It is not possible to infer from a file's suffix (if indeed it has one) what the contents of the file represent. Indeed, UNIX makes no stipulation of any sort as to what may or may not be stored in a file - a file is merely a sequence of bytes.

However, all is not lost. It is possible to make an intelligent guess as to what a file contains by examining the format of the data inside it. For instance, if the file contains words that occur in the C language, one might reasonably guess that the file contains C source code. Many sorts of data can have their type inferred from their format, and the command file is provided to do this. For example:

file prog.c
prog.c:         c program text
file libc.a
libc.a:         archive random library
file story
story:          English text

Don't worry about file telling you that a particular file is of a weird type you've never heard of! It probably means it contains something you aren't interested in anyway. Unfortunately, file is not infallible, and it is possible to confuse it, but nonetheless it's a pretty reliable aid.

Worked example

What type of file is /usr?
Solution: Use file:

file /usr
/usr:          directory

thus /usr is a directory.

We should also ask whether the question 'What does file X contain?' is a useful question. It is possible to define in a precise way what a C program must look like, but not, for instance, the data for transactions pertaining to a bank account. Rather than asking what type of data is in a particular file, we should instead be creating files whose contents are in a specified format.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck