Displaying the file type (file command)

Use the file command to read the files specified by the File or -fFileList parameter, perform a series of tests on each one, and attempt to classify the files by type. The command then writes the file types to standard output.

If a file appears to be ASCII, the file command examines the first 512 bytes and determines its language. If a file does not appear to be ASCII, the file command further attempts to determine whether it is a binary data file or a text file that contains extended characters.

If the File parameter specifies an executable or object module file and the version number is greater than 0, the file command displays the version stamp.

The file command uses the /etc/magic file to identify files that have a magic number; that is, any file containing a numeric or string constant that indicates the type.

The following are examples of how to use the file command:
  • To display the type of information the file named myfile contains, type the following:
    file myfile
    This displays the file type of myfile (such as directory, data, ASCII text, C program source, or archive).
  • To display the type of each file named in the filenames.lst file, which contains a list of file names, type the following:
    file -f filenames.lst
    This displays the type of each file named in the filenames.lst file. Each file name must display on a separate line.
  • To create the filenames.lst file that contains all the file names in the current directory, type the following:
    ls > filenames.lst
    Edit the filenames.lst file as desired.