1FILE(1) BSD General Commands Manual FILE(1)
2
4 file — determine file type
5
7 file [-bcdEhiklLNnprsSvzZ0] [--apple] [--exclude-quiet] [--extension]
8 [--mime-encoding] [--mime-type] [-e testname] [-F separator]
9 [-f namefile] [-m magicfiles] [-P name=value] file ...
10 file -C [-m magicfiles]
11 file [--help]
12
14 This manual page documents version 5.44 of the file command.
15
16 file tests each argument in an attempt to classify it. There are three
17 sets of tests, performed in this order: filesystem tests, magic tests,
18 and language tests. The first test that succeeds causes the file type to
19 be printed.
20
21 The type printed will usually contain one of the words text (the file
22 contains only printing characters and a few common control characters and
23 is probably safe to read on an ASCII terminal), executable (the file con‐
24 tains the result of compiling a program in a form understandable to some
25 UNIX kernel or another), or data meaning anything else (data is usually
26 “binary” or non-printable). Exceptions are well-known file formats (core
27 files, tar archives) that are known to contain binary data. When modify‐
28 ing magic files or the program itself, make sure to preserve these
29 keywords. Users depend on knowing that all the readable files in a di‐
30 rectory have the word “text” printed. Don't do as Berkeley did and
31 change “shell commands text” to “shell script”.
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 tests are used to check for files with data in particular fixed
41 formats. The canonical example of this is a binary executable (compiled
42 program) a.out file, whose format is defined in <elf.h>, <a.out.h> and
43 possibly <exec.h> in the standard include directory. These files have a
44 “magic number” stored in a particular place near the beginning of the
45 file that tells the UNIX operating system that the file is a binary exe‐
46 cutable, and which of several types thereof. The concept of a “magic
47 number” has been applied by extension to data files. Any file with some
48 invariant identifier at a small fixed offset into the file can usually be
49 described in this way. The information identifying these files is read
50 from the compiled magic file /usr/share/misc/magic.mgc, or the files in
51 the directory /usr/share/misc/magic if the compiled file does not exist.
52 In addition, if $HOME/.magic.mgc or $HOME/.magic exists, it will be used
53 in preference to the system magic files.
54
55 If a file does not match any of the entries in the magic file, it is ex‐
56 amined to see if it seems to be a text file. ASCII, ISO-8859-x, non-ISO
57 8-bit extended-ASCII character sets (such as those used on Macintosh and
58 IBM PC systems), UTF-8-encoded Unicode, UTF-16-encoded Unicode, and
59 EBCDIC character sets can be distinguished by the different ranges and
60 sequences of bytes that constitute printable text in each set. If a file
61 passes any of these tests, its character set is reported. ASCII,
62 ISO-8859-x, UTF-8, and extended-ASCII files are identified as “text” be‐
63 cause they will be mostly readable on nearly any terminal; UTF-16 and
64 EBCDIC are only “character data” because, while they contain text, it is
65 text that will require translation before it can be read. In addition,
66 file will attempt to determine other characteristics of text-type files.
67 If the lines of a file are terminated by CR, CRLF, or NEL, instead of the
68 Unix-standard LF, this will be reported. Files that contain embedded es‐
69 cape sequences or overstriking will also be identified.
70
71 Once file has determined the character set used in a text-type file, it
72 will attempt to determine in what language the file is written. The lan‐
73 guage tests look for particular strings (cf. <names.h>) that can appear
74 anywhere in the first few blocks of a file. For example, the keyword .br
75 indicates that the file is most likely a troff(1) input file, just as the
76 keyword struct indicates a C program. These tests are less reliable than
77 the previous two groups, so they are performed last. The language test
78 routines also test for some miscellany (such as tar(1) archives, JSON
79 files).
80
81 Any file that cannot be identified as having been written in any of the
82 character sets listed above is simply said to be “data”.
83
85 --apple
86 Causes the file command to output the file type and creator code
87 as used by older MacOS versions. The code consists of eight let‐
88 ters, the first describing the file type, the latter the creator.
89 This option works properly only for file formats that have the
90 apple-style output defined.
91
92 -b, --brief
93 Do not prepend filenames to output lines (brief mode).
94
95 -C, --compile
96 Write a magic.mgc output file that contains a pre-parsed version
97 of the magic file or directory.
98
99 -c, --checking-printout
100 Cause a checking printout of the parsed form of the magic file.
101 This is usually used in conjunction with the -m option to debug a
102 new magic file before installing it.
103
104 -d Prints internal debugging information to stderr.
105
106 -E On filesystem errors (file not found etc), instead of handling
107 the error as regular output as POSIX mandates and keep going, is‐
108 sue an error message and exit.
109
110 -e, --exclude testname
111 Exclude the test named in testname from the list of tests made to
112 determine the file type. Valid test names are:
113
114 apptype EMX application type (only on EMX).
115
116 ascii Various types of text files (this test will try to
117 guess the text encoding, irrespective of the setting of
118 the ‘encoding’ option).
119
120 encoding Different text encodings for soft magic tests.
121
122 tokens Ignored for backwards compatibility.
123
124 cdf Prints details of Compound Document Files.
125
126 compress Checks for, and looks inside, compressed files.
127
128 csv Checks Comma Separated Value files.
129
130 elf Prints ELF file details, provided soft magic tests are
131 enabled and the elf magic is found.
132
133 json Examines JSON (RFC-7159) files by parsing them for com‐
134 pliance.
135
136 soft Consults magic files.
137
138 tar Examines tar files by verifying the checksum of the 512
139 byte tar header. Excluding this test can provide more
140 detailed content description by using the soft magic
141 method.
142
143 text A synonym for ‘ascii’.
144
145 --exclude-quiet
146 Like --exclude but ignore tests that file does not know about.
147 This is intended for compatibility with older versions of file.
148
149 --extension
150 Print a slash-separated list of valid extensions for the file
151 type found.
152
153 -F, --separator separator
154 Use the specified string as the separator between the filename
155 and the file result returned. Defaults to ‘:’.
156
157 -f, --files-from namefile
158 Read the names of the files to be examined from namefile (one per
159 line) before the argument list. Either namefile or at least one
160 filename argument must be present; to test the standard input,
161 use ‘-’ as a filename argument. Please note that namefile is un‐
162 wrapped and the enclosed filenames are processed when this option
163 is encountered and before any further options processing is done.
164 This allows one to process multiple lists of files with different
165 command line arguments on the same file invocation. Thus if you
166 want to set the delimiter, you need to do it before you specify
167 the list of files, like: “-F @ -f namefile”, instead of: “-f
168 namefile -F @”.
169
170 -h, --no-dereference
171 This option causes symlinks not to be followed (on systems that
172 support symbolic links). This is the default if the environment
173 variable POSIXLY_CORRECT is not defined.
174
175 -i, --mime
176 Causes the file command to output mime type strings rather than
177 the more traditional human readable ones. Thus it may say
178 ‘text/plain; charset=us-ascii’ rather than “ASCII text”.
179
180 --mime-type, --mime-encoding
181 Like -i, but print only the specified element(s).
182
183 -k, --keep-going
184 Don't stop at the first match, keep going. Subsequent matches
185 will be have the string ‘\012- ’ prepended. (If you want a new‐
186 line, see the -r option.) The magic pattern with the highest
187 strength (see the -l option) comes first.
188
189 -l, --list
190 Shows a list of patterns and their strength sorted descending by
191 magic(5) strength which is used for the matching (see also the -k
192 option).
193
194 -L, --dereference
195 This option causes symlinks to be followed, as the like-named op‐
196 tion in ls(1) (on systems that support symbolic links). This is
197 the default if the environment variable POSIXLY_CORRECT is de‐
198 fined.
199
200 -m, --magic-file magicfiles
201 Specify an alternate list of files and directories containing
202 magic. This can be a single item, or a colon-separated list. If
203 a compiled magic file is found alongside a file or directory, it
204 will be used instead.
205
206 -N, --no-pad
207 Don't pad filenames so that they align in the output.
208
209 -n, --no-buffer
210 Force stdout to be flushed after checking each file. This is
211 only useful if checking a list of files. It is intended to be
212 used by programs that want filetype output from a pipe.
213
214 -p, --preserve-date
215 On systems that support utime(3) or utimes(2), attempt to pre‐
216 serve the access time of files analyzed, to pretend that file
217 never read them.
218
219 -P, --parameter name=value
220 Set various parameter limits.
221
222 Name Default Explanation
223 bytes 1048576 max number of bytes to read from
224 file
225 elf_notes 256 max ELF notes processed
226 elf_phnum 2048 max ELF program sections processed
227 elf_shnum 32768 max ELF sections processed
228 encoding 65536 max number of bytes to scan for
229 encoding
230 evaluation
231 indir 50 recursion limit for indirect magic
232 name 50 use count limit for name/use magic
233 regex 8192 length limit for regex searches
234
235 -r, --raw
236 Don't translate unprintable characters to \ooo. Normally file
237 translates unprintable characters to their octal representation.
238
239 -s, --special-files
240 Normally, file only attempts to read and determine the type of
241 argument files which stat(2) reports are ordinary files. This
242 prevents problems, because reading special files may have pecu‐
243 liar consequences. Specifying the -s option causes file to also
244 read argument files which are block or character special files.
245 This is useful for determining the filesystem types of the data
246 in raw disk partitions, which are block special files. This op‐
247 tion also causes file to disregard the file size as reported by
248 stat(2) since on some systems it reports a zero size for raw disk
249 partitions.
250
251 -S, --no-sandbox
252 On systems where libseccomp
253 (https://github.com/seccomp/libseccomp) is available, the -S op‐
254 tion disables sandboxing which is enabled by default. This op‐
255 tion is needed for file to execute external decompressing pro‐
256 grams, i.e. when the -z option is specified and the built-in de‐
257 compressors are not available. On systems where sandboxing is
258 not available, this option has no effect.
259
260 -v, --version
261 Print the version of the program and exit.
262
263 -z, --uncompress
264 Try to look inside compressed files.
265
266 -Z, --uncompress-noreport
267 Try to look inside compressed files, but report information about
268 the contents only not the compression.
269
270 -0, --print0
271 Output a null character ‘\0’ after the end of the filename. Nice
272 to cut(1) the output. This does not affect the separator, which
273 is still printed.
274
275 If this option is repeated more than once, then file prints just
276 the filename followed by a NUL followed by the description (or
277 ERROR: text) followed by a second NUL for each entry.
278
279 --help Print a help message and exit.
280
282 The environment variable MAGIC can be used to set the default magic file
283 name. If that variable is set, then file will not attempt to open
284 $HOME/.magic. file adds “.mgc” to the value of this variable as appro‐
285 priate. The environment variable POSIXLY_CORRECT controls (on systems
286 that support symbolic links), whether file will attempt to follow sym‐
287 links or not. If set, then file follows symlink, otherwise it does not.
288 This is also controlled by the -L and -h options.
289
291 /usr/share/misc/magic.mgc Default compiled list of magic.
292 /usr/share/misc/magic Directory containing default magic files.
293
295 file will exit with 0 if the operation was successful or >0 if an error
296 was encountered. The following errors cause diagnostic messages, but
297 don't affect the program exit code (as POSIX requires), unless -E is
298 specified:
299 • A file cannot be found
300 • There is no permission to read a file
301 • The file type cannot be determined
302
304 $ file file.c file /dev/{wd0a,hda}
305 file.c: C program text
306 file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
307 dynamically linked (uses shared libs), stripped
308 /dev/wd0a: block special (0/0)
309 /dev/hda: block special (3/0)
310
311 $ file -s /dev/wd0{b,d}
312 /dev/wd0b: data
313 /dev/wd0d: x86 boot sector
314
315 $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
316 /dev/hda: x86 boot sector
317 /dev/hda1: Linux/i386 ext2 filesystem
318 /dev/hda2: x86 boot sector
319 /dev/hda3: x86 boot sector, extended partition table
320 /dev/hda4: Linux/i386 ext2 filesystem
321 /dev/hda5: Linux/i386 swap file
322 /dev/hda6: Linux/i386 swap file
323 /dev/hda7: Linux/i386 swap file
324 /dev/hda8: Linux/i386 swap file
325 /dev/hda9: empty
326 /dev/hda10: empty
327
328 $ file -i file.c file /dev/{wd0a,hda}
329 file.c: text/x-c
330 file: application/x-executable
331 /dev/hda: application/x-not-regular-file
332 /dev/wd0a: application/x-not-regular-file
333
334
336 hexdump(1), od(1), strings(1), magic(5)
337
339 This program is believed to exceed the System V Interface Definition of
340 FILE(CMD), as near as one can determine from the vague language contained
341 therein. Its behavior is mostly compatible with the System V program of
342 the same name. This version knows more magic, however, so it will pro‐
343 duce different (albeit more accurate) output in many cases.
344
345 The one significant difference between this version and System V is that
346 this version treats any white space as a delimiter, so that spaces in
347 pattern strings must be escaped. For example,
348
349 >10 string language impress (imPRESS data)
350
351 in an existing magic file would have to be changed to
352
353 >10 string language\ impress (imPRESS data)
354
355 In addition, in this version, if a pattern string contains a backslash,
356 it must be escaped. For example
357
358 0 string \begindata Andrew Toolkit document
359
360 in an existing magic file would have to be changed to
361
362 0 string \\begindata Andrew Toolkit document
363
364 SunOS releases 3.2 and later from Sun Microsystems include a file command
365 derived from the System V one, but with some extensions. This version
366 differs from Sun's only in minor ways. It includes the extension of the
367 ‘&’ operator, used as, for example,
368
369 >16 long&0x7fffffff >0 not stripped
370
372 On systems where libseccomp (https://github.com/seccomp/libseccomp) is
373 available, file is enforces limiting system calls to only the ones neces‐
374 sary for the operation of the program. This enforcement does not provide
375 any security benefit when file is asked to decompress input files running
376 external programs with the -z option. To enable execution of external
377 decompressors, one needs to disable sandboxing using the -S option.
378
380 The magic file entries have been collected from various sources, mainly
381 USENET, and contributed by various authors. Christos Zoulas (address be‐
382 low) will collect additional or corrected magic file entries. A consoli‐
383 dation of magic file entries will be distributed periodically.
384
385 The order of entries in the magic file is significant. Depending on what
386 system you are using, the order that they are put together may be incor‐
387 rect. If your old file command uses a magic file, keep the old magic
388 file around for comparison purposes (rename it to
389 /usr/share/misc/magic.orig).
390
392 There has been a file command in every UNIX since at least Research
393 Version 4 (man page dated November, 1973). The System V version intro‐
394 duced one significant major change: the external list of magic types.
395 This slowed the program down slightly but made it a lot more flexible.
396
397 This program, based on the System V version, was written by Ian Darwin
398 ⟨ian@darwinsys.com⟩ without looking at anybody else's source code.
399
400 John Gilmore revised the code extensively, making it better than the
401 first version. Geoff Collyer found several inadequacies and provided
402 some magic file entries. Contributions of the ‘&’ operator by Rob McMa‐
403 hon, ⟨cudcv@warwick.ac.uk⟩, 1989.
404
405 Guy Harris, ⟨guy@netapp.com⟩, made many changes from 1993 to the present.
406
407 Primary development and maintenance from 1990 to the present by Christos
408 Zoulas ⟨christos@astron.com⟩.
409
410 Altered by Chris Lowth ⟨chris@lowth.com⟩, 2000: handle the -i option to
411 output mime type strings, using an alternative magic file and internal
412 logic.
413
414 Altered by Eric Fischer ⟨enf@pobox.com⟩, July, 2000, to identify charac‐
415 ter codes and attempt to identify the languages of non-ASCII files.
416
417 Altered by Reuben Thomas ⟨rrt@sc3d.org⟩, 2007-2011, to improve MIME sup‐
418 port, merge MIME and non-MIME magic, support directories as well as files
419 of magic, apply many bug fixes, update and fix a lot of magic, improve
420 the build system, improve the documentation, and rewrite the Python bind‐
421 ings in pure Python.
422
423 The list of contributors to the ‘magic’ directory (magic files) is too
424 long to include here. You know who you are; thank you. Many contribu‐
425 tors are listed in the source files.
426
428 Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999. Covered by the
429 standard Berkeley Software Distribution copyright; see the file COPYING
430 in the source distribution.
431
432 The files tar.h and is_tar.c were written by John Gilmore from his pub‐
433 lic-domain tar(1) program, and are not covered by the above license.
434
436 Please report bugs and send patches to the bug tracker at
437 https://bugs.astron.com/ or the mailing list at ⟨file@astron.com⟩ (visit
438 https://mailman.astron.com/mailman/listinfo/file first to subscribe).
439
441 Fix output so that tests for MIME and APPLE flags are not needed all over
442 the place, and actual output is only done in one place. This needs a de‐
443 sign. Suggestion: push possible outputs on to a list, then pick the
444 last-pushed (most specific, one hopes) value at the end, or use a default
445 if the list is empty. This should not slow down evaluation.
446
447 The handling of MAGIC_CONTINUE and printing \012- between entries is
448 clumsy and complicated; refactor and centralize.
449
450 Some of the encoding logic is hard-coded in encoding.c and can be moved
451 to the magic files if we had a !:charset annotation.
452
453 Continue to squash all magic bugs. See Debian BTS for a good source.
454
455 Store arbitrarily long strings, for example for %s patterns, so that they
456 can be printed out. Fixes Debian bug #271672. This can be done by allo‐
457 cating strings in a string pool, storing the string pool at the end of
458 the magic file and converting all the string pointers to relative offsets
459 from the string pool.
460
461 Add syntax for relative offsets after current level (Debian bug #466037).
462
463 Make file -ki work, i.e. give multiple MIME types.
464
465 Add a zip library so we can peek inside Office2007 documents to print
466 more details about their contents.
467
468 Add an option to print URLs for the sources of the file descriptions.
469
470 Combine script searches and add a way to map executable names to MIME
471 types (e.g. have a magic value for !:mime which causes the resulting
472 string to be looked up in a table). This would avoid adding the same
473 magic repeatedly for each new hash-bang interpreter.
474
475 When a file descriptor is available, we can skip and adjust the buffer
476 instead of the hacky buffer management we do now.
477
478 Fix “name” and “use” to check for consistency at compile time (duplicate
479 “name”, “use” pointing to undefined “name” ). Make “name” / “use” more
480 efficient by keeping a sorted list of names. Special-case ^ to flip en‐
481 dianness in the parser so that it does not have to be escaped, and docu‐
482 ment it.
483
484 If the offsets specified internally in the file exceed the buffer size (
485 HOWMANY variable in file.h), then we don't seek to that offset, but we
486 give up. It would be better if buffer managements was done when the file
487 descriptor is available so we can seek around the file. One must be
488 careful though because this has performance and thus security considera‐
489 tions, because one can slow down things by repeatedly seeking.
490
491 There is support now for keeping separate buffers and having offsets from
492 the end of the file, but the internal buffer management still needs an
493 overhaul.
494
496 You can obtain the original author's latest version by anonymous FTP on
497 ftp.astron.com in the directory /pub/file/file-X.YZ.tar.gz.
498
499BSD October 26, 2022 BSD