Previous Table of Contents Next

Module 53


The file command determines what type of data a file contains. It returns a brief explanation of what type of data it believes is in each file.


Following are the general formats of the file command.

     file [ -h ] [ -m magic_file ] file_list
     file [ -h ] [ -m magic_file ] -f list_file
     file -c     [ -m magic_file ]

BSD (Berkeley)
file filename


The following list describes the options and their arguments that may be used to control how file functions.

-c Check the magic file for format errors. Usually not used unless you are writing a new magic file. No file type determination is performed if -c is specified.
-f list_file The next argument after the -f is read and the contents are used as the pathnames for files to be examined by file.
-h Do not follow symbolic links.
-m magic_file The next argument after the -m is used as the magic file instead of the default /etc/magic file.

BSD (Berkeley)
Does not support any options.


The following list describes the arguments passed to the file command.

file_list One or more files to be checked by file to determine the type of data stored in them.
list_file The file containing the pathnames of the files to be checked by file.
magic_file The file to be used in place of the /etc/magic file. It is a good idea to use the -c option on this file until you remove all format errors.

BSD (Berkeley)
Does not support list_files or magic_files.


The file command is useful for knowing what type of data a file contains. It can help prevent you from locking up your terminal by informing you that a file contains executable program text. If you cat an executable file, your terminal may act strange and, in fact, the keyboard often becomes disabled. The only way out of the situation is to turn your terminal off and back on, then log back in to the system. If you perform file before you cat a file, you will know what type of data it contains and whether or not you can display it.

The file command makes calculated assumptions as to what type of data is in a file. The following list describes the types file may return. Keep in mind file is trying to guess what type of data is in a file. The known system types of data, such as executable or linkable object code, are known but some shell scripts and English text files may confuse file and thus an incorrect response is returned.

Type of data Description

MACHID executable Compiled code ready to execute
ASCII text ASCII characters
c program text C source code
commands text Shell script language
English text Plain English text
Fortran text Fortran source code
BASIC program text BASIC source code
COBOL program text COBOL source code
directory A directory file
empty No data exist in the file

The description given by the file command you are using may vary but it should inform you of the same type of data stored in the file. You can display the /etc/magic file to see the definitions used by your system's file command.

The file command is not always correct in its assumption of the type of data stored in a file. The most common mistakes of file are the ASCII text, English text, and commands text.


The /etc/magic file is used as a cross reference to determine what type of data is stored in a file. The magic number is based on numeric or string constants stored within the file itself. The format of the /etc/magic file is described in the first lines of the file (/etc/magic).

The file command writes to the standard output. Its output may be piped to another command or redirected to a file.


Use the file command to determine what type of data a file contains. You can then classify the file and view it using the cat, pg, or vi command if it is not an executable or data file.


In this activity you use the file command to view the different types of files in the /etc directory. Begin at the shell prompt.

1.  Type file /etc/* and press Return. Notice each file is displayed with a brief description to the right.

BSD (Berkeley)
Since file cannot function on multiple files you must use a loop to check each file. Some BSD systems have changed file so it does support multiple filenames on the command line.
cj> foreach F (/etc/*)
file $F

2.  To see what your directory contains for files type file * and press Return. Your screen should resemble the following list.
     cj> file *
     bin:      directory
     calendar:       ascii text
     db:  directory
     file1: English text
     letters:    directory
     myfile: empty

BSD (Berkeley)
Must use a foreach F (*) loop.

3.  Turn to Module 13 to continue the learning sequence.

Previous Table of Contents Next