Section: User Commands (1)
Index Return to Main Contents

BSD mandoc


file - determine file type  


-words [-bchikLNnprsvz0 ] [--apple ] [--mime-encoding ] [--mime-type ] [-e testname ] [-F separator ] [-f namefile ] [-m magicfiles ] file ... -words
-C [-m magicfiles ]
[--help ]  


This manual page documents version 5.04 of the command.









-b , -brief
Do not prepend filenames to output lines (brief mode).
-C , -compile
-c , -checking-printout
-e , -exclude testname

EMX application type (only on EMX).
Different text encodings for soft magic tests.
Looks for known tokens inside text files.
Prints details of Compound Document Files.
Checks for, and looks inside, compressed files.
Prints ELF file details.
Consults magic files.
Examines tar files.

-F , -separator separator
-f , -files-from namefile
-h , -no-dereference
-i , -mime
Causes the file command to output mime type strings rather than the more traditional human readable ones. Thus it may say `text/plain; charset=us-ascii' rather than `ASCII text' In order for this option to work, file changes the way it handles files recognized by the command itself (such as many of the text file types, directories etc), and makes use of an alternative `magic' file. (See the FILES section, below).
--mime-type , -mime-encoding
-k , -keep-going
Don't stop at the first match, keep going. Subsequent matches will be have the string `[rs]012- ' prepended. (If you want a newline, see the `-r' option.)
-L , -dereference
-m , -magic-file magicfiles
-N , -no-pad
Don't pad filenames so that they align in the output.
-n , -no-buffer
-p , -preserve-date
-r , -raw
-s , -special-files
-v , -version
Print the version of the program and exit.
-z , -uncompress
Try to look inside compressed files.
-0 , -print0
Print a help message and exit.



Default compiled list of magic.
Directory containing default magic files.



The environment variable MAGIC can be used to set the default magic file name. If that variable is set, then will not attempt to open $HOME/.magic adds `.mgc' to the value of this variable as appropriate. The environment variable POSIXLY_CORRECT controls (on systems that support symbolic links), whether will attempt to follow symlinks or not. If set, then follows symlink, otherwise it does not. This is also controlled by the -L and -h options.  


magic(5), strings(1), od(1), hexdump(1), file(1posix)  




>10     string  language impress        (imPRESS data)

in an existing magic file would have to be changed to

>10     string  language\ impress       (imPRESS data)


0       string          \begindata      Andrew Toolkit document

in an existing magic file would have to be changed to

0       string          \\begindata     Andrew Toolkit document

SunOS releases 3.2 and later from Sun Microsystems include a command derived from the System V one, but with some extensions. My version differs from Sun's only in minor ways. It includes the extension of the `&' operator, used as, for example,

>16     long&0x7fffffff >0              not stripped





$ file file.c file /dev/{wd0a,hda}
file.c:   C program text
          dynamically linked (uses shared libs), stripped
/dev/wd0a: block special (0/0)
/dev/hda: block special (3/0)

/dev/wd0b: data
/dev/wd0d: x86 boot sector

$ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
/dev/hda:   x86 boot sector
/dev/hda1:  Linux/i386 ext2 filesystem
/dev/hda2:  x86 boot sector
/dev/hda3:  x86 boot sector, extended partition table
/dev/hda4:  Linux/i386 ext2 filesystem
/dev/hda5:  Linux/i386 swap file
/dev/hda6:  Linux/i386 swap file
/dev/hda7:  Linux/i386 swap file
/dev/hda8:  Linux/i386 swap file
/dev/hda9:  empty
/dev/hda10: empty

file.c:      text/x-c
file:        application/x-executable




This program, based on the System V version, was written by Ian Darwin <[email protected]> without looking at anybody else's source code.

John Gilmore revised the code extensively, making it better than the first version. Geoff Collyer found several inadequacies and provided some magic file entries. Contributions by the `&' operator by Rob McMahon, [email protected], 1989.

Guy Harris, [email protected], made many changes from 1993 to the present.

Primary development and maintenance from 1990 to the present by Christos Zoulas ([email protected]).

Altered by Chris Lowth, [email protected], 2000: Handle the -i option to output mime type strings, using an alternative magic file and internal logic.

Altered by Eric Fischer ([email protected]), July, 2000, to identify character codes and attempt to identify the languages of non-ASCII files.

Altered by Reuben Thomas ([email protected]), 2007 to 2008, to improve MIME support and merge MIME and non-MIME magic, support directories as well as files of magic, apply many bug fixes and improve the build system.



Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999. Covered by the standard Berkeley Software Distribution copyright; see the file LEGAL.NOTICE in the source distribution.



There must be a better way to automate the construction of the Magic file from all the glop in Magdir. What is it?

uses several algorithms that favor speed over accuracy, thus it can be misled about the contents of text files.

The support for text files (primarily for programming languages) is simplistic, inefficient and requires recompilation to update.

The list of keywords in ascmagic probably belongs in the Magic file. This could be done by using some keyword like `*' for the offset value.

Complain about conflicts in the magic file entries. Make a rule that the magic entries sort based on file offset rather than position within the magic file?

The program should provide a way to give an estimate of `how good' a guess is. We end up removing guesses (e.g. `Fromas first 5 chars of file) because' they are not as good as other guesses (e.g. `Newsgroups:' versus `Return-Path:' ). Still, if the others don't pan out, it should be possible to use the first guess.



returns 0 on success, and non-zero on error.

If the file named by the file operand does not exist, cannot be read, or the type of the file named by the file operand cannot be determined, this is not be considered an error that affects the exit status.  






This document was created by man2html, using the manual pages.
Time: 05:29:03 GMT, December 24, 2015