1FILE(1) BSD General Commands Manual FILE(1)
2
4 file — determine file type
5
7 file [-bchikLnNprsvz] [-f namefile] [-F separator] [-m magicfiles] file
8 file -C [-m magicfile]
9
11 This manual page documents version 4.21 of the file command.
12
13 file tests each argument in an attempt to classify it. There are three
14 sets of tests, performed in this order: filesystem tests, magic number
15 tests, and language tests. The first test that succeeds causes the file
16 type to be printed.
17
18 The type printed will usually contain one of the words text (the file
19 contains only printing characters and a few common control characters and
20 is probably safe to read on an ASCII terminal), executable (the file con‐
21 tains the result of compiling a program in a form understandable to some
22 UNIX kernel or another), or data meaning anything else (data is usually
23 ‘binary’ or non-printable). Exceptions are well-known file formats (core
24 files, tar archives) that are known to contain binary data. When modify‐
25 ing the file /usr/share/file/magic or the program itself, make sure to
26 preserve these keywords. People depend on knowing that all the readable
27 files in a directory have the word “text” printed. Don't do as Berkeley
28 did and change “shell commands text” to “shell script”. Note that the
29 file /usr/share/file/magic is built mechanically from a large number of
30 small files in the subdirectory Magdir in the source distribution of this
31 program.
32
33 The filesystem tests are based on examining the return from a stat(2)
34 system call. The program checks to see if the file is empty, or if it's
35 some sort of special file. Any known file types appropriate to the sys‐
36 tem you are running on (sockets, symbolic links, or named pipes (FIFOs)
37 on those systems that implement them) are intuited if they are defined in
38 the system header file <sys/stat.h>.
39
40 The magic number tests are used to check for files with data in particu‐
41 lar fixed formats. The canonical example of this is a binary executable
42 (compiled program) a.out file, whose format is defined in <elf.h>,
43 <a.out.h> and possibly <exec.h> in the standard include directory. These
44 files have a ‘magic number’ stored in a particular place near the begin‐
45 ning of the file that tells the UNIX operating system that the file is a
46 binary executable, and which of several types thereof. The concept of a
47 ‘magic number’ has been applied by extension to data files. Any file
48 with some invariant identifier at a small fixed offset into the file can
49 usually be described in this way. The information identifying these
50 files is read from the compiled magic file /usr/share/file/magic.mgc, or
51 /usr/share/file/magic if the compile file does not exist. In addition
52 file will look in $HOME/.magic.mgc, or $HOME/.magic for magic entries.
53
54 If a file does not match any of the entries in the magic file, it is
55 examined to see if it seems to be a text file. ASCII, ISO-8859-x, non-
56 ISO 8-bit extended-ASCII character sets (such as those used on Macintosh
57 and IBM PC systems), UTF-8-encoded Unicode, UTF-16-encoded Unicode, and
58 EBCDIC character sets can be distinguished by the different ranges and
59 sequences of bytes that constitute printable text in each set. If a file
60 passes any of these tests, its character set is reported. ASCII,
61 ISO-8859-x, UTF-8, and extended-ASCII files are identified as “text”
62 because they will be mostly readable on nearly any terminal; UTF-16 and
63 EBCDIC are only “character data” because, while they contain text, it is
64 text that will require translation before it can be read. In addition,
65 file will attempt to determine other characteristics of text-type files.
66 If the lines of a file are terminated by CR, CRLF, or NEL, instead of the
67 Unix-standard LF, this will be reported. Files that contain embedded
68 escape sequences or overstriking will also be identified.
69
70 Once file has determined the character set used in a text-type file, it
71 will attempt to determine in what language the file is written. The lan‐
72 guage tests look for particular strings (cf <names.h> that can appear
73 anywhere in the first few blocks of a file. For example, the keyword .br
74 indicates that the file is most likely a troff(1) input file, just as the
75 keyword struct indicates a C program. These tests are less reliable than
76 the previous two groups, so they are performed last. The language test
77 routines also test for some miscellany (such as tar(1) archives).
78
79 Any file that cannot be identified as having been written in any of the
80 character sets listed above is simply said to be ``data''.
81
83 -b, --brief
84 Do not prepend filenames to output lines (brief mode).
85
86 -c, --checking-printout
87 Cause a checking printout of the parsed form of the magic file.
88 This is usually used in conjunction with the -m flag to debug a
89 new magic file before installing it.
90
91 -C, --compile
92 Write a magic.mgc output file that contains a pre-parsed version
93 of the magic file.
94
95 -e, --exclude testname
96 Exclude the test named in testname from the list of tests made to
97 determine the file type. Valid test names are:
98
99 apptype
100 Check for EMX application type (only on EMX).
101
102 ascii
103 Check for various types of ascii files.
104
105 compress
106 Don't look for, or inside compressed files.
107
108 elf
109 Don't print elf details.
110
111 fortran
112 Don't look for fortran sequences inside ascii files.
113
114 soft
115 Don't consult magic files.
116
117 tar
118 Don't examine tar files.
119
120 token
121 Don't look for known tokens inside ascii files.
122
123 troff
124 Don't look for troff sequences inside ascii files.
125
126 -f, --files-from namefile
127 Read the names of the files to be examined from namefile (one per
128 line) before the argument list. Either namefile or at least one
129 filename argument must be present; to test the standard input,
130 use ‘-’ as a filename argument.
131
132 -F, --separator separator
133 Use the specified string as the separator between the filename
134 and the file result returned. Defaults to ‘:’.
135
136 -h, --no-dereference
137 option causes symlinks not to be followed (on systems that sup‐
138 port symbolic links). This is the default if the environment
139 variable POSIXLY_CORRECT is not defined.
140
141 -i, --mime
142 Causes the file command to output mime type strings rather than
143 the more traditional human readable ones. Thus it may say
144 “text/plain; charset=us-ascii” rather than “ASCII text”. In
145 order for this option to work, file changes the way it handles
146 files recognized by the command itself (such as many of the text
147 file types, directories etc), and makes use of an alternative
148 “magic” file. (See “FILES” section, below).
149
150 -k, --keep-going
151 Don't stop at the first match, keep going.
152
153 -L, --dereference
154 option causes symlinks to be followed, as the like-named option
155 in ls(1) (on systems that support symbolic links). This is the
156 default if the environment variable POSIXLY_CORRECT is defined.
157
158 -m, --magic-file list
159 Specify an alternate list of files containing magic numbers.
160 This can be a single file, or a colon-separated list of files.
161 If a compiled magic file is found alongside, it will be used
162 instead. With the -i -or -mime option, the program adds “.mime”
163 to each file name.
164
165 -n, --no-buffer
166 Force stdout to be flushed after checking each file. This is
167 only useful if checking a list of files. It is intended to be
168 used by programs that want filetype output from a pipe.
169
170 -N, --no-pad
171 Don't pad filenames so that they align in the output.
172
173 -p, --preserve-date
174 On systems that support utime(2) or utimes(2), attempt to pre‐
175 serve the access time of files analyzed, to pretend that file
176 never read them.
177
178 -r, --raw
179 Don't translate unprintable characters to \ooo. Normally file
180 translates unprintable characters to their octal representation.
181
182 -s, --special-files
183 Normally, file only attempts to read and determine the type of
184 argument files which stat(2) reports are ordinary files. This
185 prevents problems, because reading special files may have pecu‐
186 liar consequences. Specifying the -s option causes file to also
187 read argument files which are block or character special files.
188 This is useful for determining the filesystem types of the data
189 in raw disk partitions, which are block special files. This
190 option also causes file to disregard the file size as reported by
191 stat(2) since on some systems it reports a zero size for raw disk
192 partitions.
193
194 -v, --version
195 Print the version of the program and exit.
196
197 -z, --uncompress
198 Try to look inside compressed files.
199
200 -0, --print0
201 Output a null character ‘\0’ after the end of the filename. Nice
202 to cut(1) the output. This does not affect the separator which is
203 still printed.
204
205 --help Print a help message and exit.
206
208 /usr/share/file/magic.mgc Default compiled list of magic numbers
209 /usr/share/file/magic Default list of magic numbers
210 /usr/share/file/magic.mime.mgc Default compiled list of magic numbers,
211 used to output mime types when the -i
212 option is specified.
213 /usr/share/file/magic.mime Default list of magic numbers, used to
214 output mime types when the -i option is
215 specified.
216
218 The environment variable MAGIC can be used to set the default magic num‐
219 ber file name. If that variable is set, then file will not attempt to
220 open $HOME/.magic. file adds “.mime” and/or “.mgc” to the value of this
221 variable as appropriate. The environment variable POSIXLY_CORRECT con‐
222 trols (on systems that support symbolic links), if file will attempt to
223 follow symlinks or not. If set, then file follows symlink, otherwise it
224 does not. This is also controlled by the -L and -h options.
225
227 magic(5), strings(1), od(1), hexdump(1)
228
230 This program is believed to exceed the System V Interface Definition of
231 FILE(CMD), as near as one can determine from the vague language contained
232 therein. Its behavior is mostly compatible with the System V program of
233 the same name. This version knows more magic, however, so it will pro‐
234 duce different (albeit more accurate) output in many cases.
235
236 The one significant difference between this version and System V is that
237 this version treats any white space as a delimiter, so that spaces in
238 pattern strings must be escaped. For example,
239
240 >10 string language impress (imPRESS data)
241
242 in an existing magic file would have to be changed to
243
244 >10 string language\ impress (imPRESS data)
245
246 In addition, in this version, if a pattern string contains a backslash,
247 it must be escaped. For example
248
249 0 string \begindata Andrew Toolkit document
250
251 in an existing magic file would have to be changed to
252
253 0 string \\begindata Andrew Toolkit document
254
255 SunOS releases 3.2 and later from Sun Microsystems include a file command
256 derived from the System V one, but with some extensions. My version dif‐
257 fers from Sun's only in minor ways. It includes the extension of the ‘&’
258 operator, used as, for example,
259
260 >16 long&0x7fffffff >0 not stripped
261
263 The magic file entries have been collected from various sources, mainly
264 USENET, and contributed by various authors. Christos Zoulas (address
265 below) will collect additional or corrected magic file entries. A con‐
266 solidation of magic file entries will be distributed periodically.
267
268 The order of entries in the magic file is significant. Depending on what
269 system you are using, the order that they are put together may be incor‐
270 rect. If your old file command uses a magic file, keep the old magic
271 file around for comparison purposes (rename it to
272 /usr/share/file/magic.orig ).
273
275 $ file file.c file /dev/{wd0a,hda}
276 file.c: C program text
277 file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
278 dynamically linked (uses shared libs), stripped
279 /dev/wd0a: block special (0/0)
280 /dev/hda: block special (3/0)
281
282 $ file -s /dev/wd0{b,d}
283 /dev/wd0b: data
284 /dev/wd0d: x86 boot sector
285
286 $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
287 /dev/hda: x86 boot sector
288 /dev/hda1: Linux/i386 ext2 filesystem
289 /dev/hda2: x86 boot sector
290 /dev/hda3: x86 boot sector, extended partition table
291 /dev/hda4: Linux/i386 ext2 filesystem
292 /dev/hda5: Linux/i386 swap file
293 /dev/hda6: Linux/i386 swap file
294 /dev/hda7: Linux/i386 swap file
295 /dev/hda8: Linux/i386 swap file
296 /dev/hda9: empty
297 /dev/hda10: empty
298
299 $ file -i file.c file /dev/{wd0a,hda}
300 file.c: text/x-c
301 file: application/x-executable, dynamically linked (uses shared libs),
302 not stripped
303 /dev/hda: application/x-not-regular-file
304 /dev/wd0a: application/x-not-regular-file
305
306
308 There has been a file command in every UNIX since at least Research
309 Version 4 (man page dated November, 1973). The System V version intro‐
310 duced one significant major change: the external list of magic number
311 types. This slowed the program down slightly but made it a lot more
312 flexible.
313
314 This program, based on the System V version, was written by Ian Darwin
315 <ian@darwinsys.com> without looking at anybody else's source code.
316
317 John Gilmore revised the code extensively, making it better than the
318 first version. Geoff Collyer found several inadequacies and provided
319 some magic file entries. Contributions by the `&' operator by Rob McMa‐
320 hon, cudcv@warwick.ac.uk, 1989.
321
322 Guy Harris, guy@netapp.com, made many changes from 1993 to the present.
323
324 Primary development and maintenance from 1990 to the present by Christos
325 Zoulas (christos@astron.com).
326
327 Altered by Chris Lowth, chris@lowth.com, 2000: Handle the -i option to
328 output mime type strings and using an alternative magic file and internal
329 logic.
330
331 Altered by Eric Fischer (enf@pobox.com), July, 2000, to identify charac‐
332 ter codes and attempt to identify the languages of non-ASCII files.
333
334 The list of contributors to the "Magdir" directory (source for the
335 /usr/share/file/magic file) is too long to include here. You know who
336 you are; thank you.
337
339 Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999. Covered by the
340 standard Berkeley Software Distribution copyright; see the file
341 LEGAL.NOTICE in the source distribution.
342
343 The files tar.h and is_tar.c were written by John Gilmore from his pub‐
344 lic-domain tar(1) program, and are not covered by the above license.
345
347 There must be a better way to automate the construction of the Magic file
348 from all the glop in Magdir. What is it?
349
350 file uses several algorithms that favor speed over accuracy, thus it can
351 be misled about the contents of text files.
352
353 The support for text files (primarily for programming languages) is sim‐
354 plistic, inefficient and requires recompilation to update. Their use of
355 ASCII TAB as a field delimiter is ugly and makes it hard to edit the
356 files, but is entrenched.
357
358 It might be advisable to allow upper-case letters in keywords for e.g.,
359 troff(1) commands vs man page macros. Regular expression support would
360 make this easy.
361
362 The program doesn't grok FORTRAN. It should be able to figure FORTRAN by
363 seeing some keywords which appear indented at the start of line. Regular
364 expression support would make this easy.
365
366 The list of keywords in ascmagic probably belongs in the Magic file.
367 This could be done by using some keyword like ‘*’ for the offset value.
368
369 Complain about conflicts in the magic file entries. Make a rule that the
370 magic entries sort based on file offset rather than position within the
371 magic file?
372
373 The program should provide a way to give an estimate of “how good” a
374 guess is. We end up removing guesses (e.g. “From ” as first 5 chars of
375 file) because they are not as good as other guesses (e.g. “Newsgroups:”
376 versus “Return-Path:” ). Still, if the others don't pan out, it should
377 be possible to use the first guess.
378
379 This program is slower than some vendors' file commands. The new support
380 for multiple character codes makes it even slower.
381
382 This manual page, and particularly this section, is too long.
383
385 You can obtain the original author's latest version by anonymous FTP on
386 ftp.astron.com in the directory /pub/file/file-X.YZ.tar.gz
387
388BSD January 8, 2007 BSD