file.man 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. .TH FILE __CSECTION__ "January 2004" "Debian/GNU Linux" "Copyrighted but distributable"
  2. .\" $Id: file.man,v 1.54 2003/10/27 18:09:08 christos Exp $
  3. .SH NAME
  4. file
  5. \- determine file type
  6. .SH SYNOPSIS
  7. .B file
  8. [
  9. .B \-bcikLnNprsvz
  10. ]
  11. [
  12. .B \-f
  13. .I namefile
  14. ]
  15. [
  16. .B \-F
  17. .I separator
  18. ]
  19. [
  20. .B \-m
  21. .I magicfiles
  22. ]
  23. .I file
  24. \&...
  25. .br
  26. .B file
  27. .B -C
  28. [
  29. .B \-m
  30. magicfile ]
  31. .SH DESCRIPTION
  32. This manual page documents version __VERSION__ of the
  33. .B file
  34. command.
  35. .PP
  36. .B File
  37. tests each argument in an attempt to classify it.
  38. There are three sets of tests, performed in this order:
  39. filesystem tests, magic number tests, and language tests.
  40. The
  41. .I first
  42. test that succeeds causes the file type to be printed.
  43. .PP
  44. The type printed will usually contain one of the words
  45. .B text
  46. (the file contains only
  47. printing characters and a few common control
  48. characters and is probably safe to read on an
  49. .SM ASCII
  50. terminal),
  51. .B executable
  52. (the file contains the result of compiling a program
  53. in a form understandable to some \s-1UNIX\s0 kernel or another),
  54. or
  55. .B data
  56. meaning anything else (data is usually `binary' or non-printable).
  57. Exceptions are well-known file formats (core files, tar archives)
  58. that are known to contain binary data.
  59. When adding local definitions to
  60. .IR /etc/magic ,
  61. .BR "preserve these keywords" .
  62. People depend on knowing that all the readable files in a directory
  63. have the word ``text'' printed.
  64. Don't do as Berkeley did and change ``shell commands text''
  65. to ``shell script''.
  66. Note that the file
  67. .I __MAGIC__
  68. is built mechanically from a large number of small files in
  69. the subdirectory
  70. .I Magdir
  71. in the source distribution of this program.
  72. .PP
  73. The filesystem tests are based on examining the return from a
  74. .BR stat (2)
  75. system call.
  76. The program checks to see if the file is empty,
  77. or if it's some sort of special file.
  78. Any known file types appropriate to the system you are running on
  79. (sockets, symbolic links, or named pipes (FIFOs) on those systems that
  80. implement them)
  81. are intuited if they are defined in
  82. the system header file
  83. .IR <sys/stat.h> .
  84. .PP
  85. The magic number tests are used to check for files with data in
  86. particular fixed formats.
  87. The canonical example of this is a binary executable (compiled program)
  88. .I a.out
  89. file, whose format is defined in
  90. .I a.out.h
  91. and possibly
  92. .I exec.h
  93. in the standard include directory.
  94. These files have a `magic number' stored in a particular place
  95. near the beginning of the file that tells the \s-1UNIX\s0 operating system
  96. that the file is a binary executable, and which of several types thereof.
  97. The concept of `magic number' has been applied by extension to data files.
  98. Any file with some invariant identifier at a small fixed
  99. offset into the file can usually be described in this way.
  100. The information identifying these files is read from
  101. .I /etc/magic
  102. and the compiled
  103. magic file
  104. .I __MAGIC__.mgc ,
  105. or
  106. .I __MAGIC__
  107. if the compile file does not exist.
  108. .PP
  109. If a file does not match any of the entries in the magic file,
  110. it is examined to see if it seems to be a text file.
  111. ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets
  112. (such as those used on Macintosh and IBM PC systems),
  113. UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC
  114. character sets can be distinguished by the different
  115. ranges and sequences of bytes that constitute printable text
  116. in each set.
  117. If a file passes any of these tests, its character set is reported.
  118. ASCII, ISO-8859-x, UTF-8, and extended-ASCII files are identified
  119. as ``text'' because they will be mostly readable on nearly any terminal;
  120. UTF-16 and EBCDIC are only ``character data'' because, while
  121. they contain text, it is text that will require translation
  122. before it can be read.
  123. In addition,
  124. .B file
  125. will attempt to determine other characteristics of text-type files.
  126. If the lines of a file are terminated by CR, CRLF, or NEL, instead
  127. of the Unix-standard LF, this will be reported.
  128. Files that contain embedded escape sequences or overstriking
  129. will also be identified.
  130. .PP
  131. Once
  132. .B file
  133. has determined the character set used in a text-type file,
  134. it will
  135. attempt to determine in what language the file is written.
  136. The language tests look for particular strings (cf
  137. .IR names.h )
  138. that can appear anywhere in the first few blocks of a file.
  139. For example, the keyword
  140. .B .br
  141. indicates that the file is most likely a
  142. .BR troff (1)
  143. input file, just as the keyword
  144. .B struct
  145. indicates a C program.
  146. These tests are less reliable than the previous
  147. two groups, so they are performed last.
  148. The language test routines also test for some miscellany
  149. (such as
  150. .BR tar (1)
  151. archives).
  152. .PP
  153. Any file that cannot be identified as having been written
  154. in any of the character sets listed above is simply said to be ``data''.
  155. .SH OPTIONS
  156. .TP 8
  157. .B "\-b, \-\-brief"
  158. Do not prepend filenames to output lines (brief mode).
  159. .TP 8
  160. .B "\-c, \-\-checking\-printout"
  161. Cause a checking printout of the parsed form of the magic file.
  162. This is usually used in conjunction with
  163. .B \-m
  164. to debug a new magic file before installing it.
  165. .TP 8
  166. .B "\-C, \-\-compile"
  167. Write a magic.mgc output file that contains a pre-parsed version of
  168. file.
  169. .TP 8
  170. .BI "\-f, \-\-files\-from" " namefile"
  171. Read the names of the files to be examined from
  172. .I namefile
  173. (one per line)
  174. before the argument list.
  175. Either
  176. .I namefile
  177. or at least one filename argument must be present;
  178. to test the standard input, use ``\-'' as a filename argument.
  179. .TP 8
  180. .BI "\-F, \-\-separator" " separator"
  181. Use the specified string as the separator between the filename and the
  182. file result returned. Defaults to ``:''.
  183. .TP 8
  184. .B "\-i, \-\-mime"
  185. Causes the file command to output mime type strings rather than the more
  186. traditional human readable ones. Thus it may say
  187. ``text/plain; charset=us-ascii''
  188. rather
  189. than ``ASCII text''.
  190. In order for this option to work, file changes the way
  191. it handles files recognised by the command itself (such as many of the
  192. text file types, directories etc), and makes use of an alternative
  193. ``magic'' file.
  194. (See ``FILES'' section, below).
  195. .TP 8
  196. .B "\-k, \-\-keep\-going"
  197. Don't stop at the first match, keep going.
  198. .TP 8
  199. .B "\-L, \-\-dereference"
  200. option causes symlinks to be followed, as the like-named option in
  201. .BR ls (1).
  202. (on systems that support symbolic links).
  203. .TP 8
  204. .BI "\-m, \-\-magic\-file" " list"
  205. Specify an alternate list of files containing magic numbers.
  206. This can be a single file, or a colon-separated list of files.
  207. If a compiled magic file is found alongside, it will be used instead.
  208. With the \-i or \-\-mime option, the program adds ".mime" to each file name.
  209. .TP 8
  210. .B "\-n, \-\-no\-buffer"
  211. Force stdout to be flushed after checking each file.
  212. This is only useful if checking a list of files.
  213. It is intended to be used by programs that want filetype output from a pipe.
  214. .TP 8
  215. .B "\-N, \-\-no\-pad"
  216. Don't pad filenames so that they align in the output.
  217. .TP 8
  218. .B "\-p, \-\-preserve\-date"
  219. On systems that support
  220. .BR utime (2)
  221. or
  222. .BR utimes(2),
  223. attempt to preserve the access time of files analyzed, to pretend that
  224. .BR file (2)
  225. never read them.
  226. .TP 8
  227. .B "\-r, \-\-raw"
  228. Don't translate unprintable characters to \eooo.
  229. Normally
  230. .B file
  231. translates unprintable characters to their octal representation.
  232. .TP 8
  233. .B "\-s, \-\-special\-files"
  234. Normally,
  235. .B file
  236. only attempts to read and determine the type of argument files which
  237. .BR stat (2)
  238. reports are ordinary files.
  239. This prevents problems, because reading special files may have peculiar
  240. consequences.
  241. Specifying the
  242. .BR \-s
  243. option causes
  244. .B file
  245. to also read argument files which are block or character special files.
  246. This is useful for determining the filesystem types of the data in raw
  247. disk partitions, which are block special files.
  248. This option also causes
  249. .B file
  250. to disregard the file size as reported by
  251. .BR stat (2)
  252. since on some systems it reports a zero size for raw disk partitions.
  253. .TP 8
  254. .B "\-v, \-\-version"
  255. Print the version of the program and exit.
  256. .TP 8
  257. .B "\-z, \-\-uncompress"
  258. Try to look inside compressed files.
  259. .TP 8
  260. .B "\-\-help"
  261. Print a help message and exit.
  262. .SH FILES
  263. .TP
  264. .I __MAGIC__.mgc
  265. Default compiled list of magic numbers
  266. .TP
  267. .I __MAGIC__
  268. Default list of magic numbers
  269. .TP
  270. .I __MAGIC__.mime.mgc
  271. Default compiled list of magic numbers, used to output mime types when
  272. the -i option is specified.
  273. .TP
  274. .I __MAGIC__.mime
  275. Default list of magic numbers, used to output mime types when the -i option
  276. is specified.
  277. .TP
  278. .I /etc/magic
  279. Local additions to magic wisdom.
  280. .SH ENVIRONMENT
  281. The environment variable
  282. .B MAGIC
  283. can be used to set the default magic number file name.
  284. .B file
  285. adds ".mime" and/or ".mgc" to the value of this variable as appropriate.
  286. .SH SEE ALSO
  287. .BR magic (__FSECTION__)
  288. \- description of magic file format.
  289. .br
  290. .BR strings (1), " od" (1), " hexdump(1)"
  291. \- tools for examining non-textfiles.
  292. .SH STANDARDS CONFORMANCE
  293. This program is believed to exceed the System V Interface Definition
  294. of FILE(CMD), as near as one can determine from the vague language
  295. contained therein.
  296. Its behaviour is mostly compatible with the System V program of the same name.
  297. This version knows more magic, however, so it will produce
  298. different (albeit more accurate) output in many cases.
  299. .PP
  300. The one significant difference
  301. between this version and System V
  302. is that this version treats any white space
  303. as a delimiter, so that spaces in pattern strings must be escaped.
  304. For example,
  305. .br
  306. >10 string language impress\ (imPRESS data)
  307. .br
  308. in an existing magic file would have to be changed to
  309. .br
  310. >10 string language\e impress (imPRESS data)
  311. .br
  312. In addition, in this version, if a pattern string contains a backslash,
  313. it must be escaped.
  314. For example
  315. .br
  316. 0 string \ebegindata Andrew Toolkit document
  317. .br
  318. in an existing magic file would have to be changed to
  319. .br
  320. 0 string \e\ebegindata Andrew Toolkit document
  321. .br
  322. .PP
  323. SunOS releases 3.2 and later from Sun Microsystems include a
  324. .BR file (1)
  325. command derived from the System V one, but with some extensions.
  326. My version differs from Sun's only in minor ways.
  327. It includes the extension of the `&' operator, used as,
  328. for example,
  329. .br
  330. >16 long&0x7fffffff >0 not stripped
  331. .SH MAGIC DIRECTORY
  332. The magic file entries have been collected from various sources,
  333. mainly USENET, and contributed by various authors.
  334. Christos Zoulas (address below) will collect additional
  335. or corrected magic file entries.
  336. A consolidation of magic file entries
  337. will be distributed periodically.
  338. .PP
  339. The order of entries in the magic file is significant.
  340. Depending on what system you are using, the order that
  341. they are put together may be incorrect.
  342. .SH EXAMPLES
  343. .nf
  344. $ file file.c file /dev/{wd0a,hda}
  345. file.c: C program text
  346. file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
  347. dynamically linked (uses shared libs), stripped
  348. /dev/wd0a: block special (0/0)
  349. /dev/hda: block special (3/0)
  350. $ file -s /dev/wd0{b,d}
  351. /dev/wd0b: data
  352. /dev/wd0d: x86 boot sector
  353. $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
  354. /dev/hda: x86 boot sector
  355. /dev/hda1: Linux/i386 ext2 filesystem
  356. /dev/hda2: x86 boot sector
  357. /dev/hda3: x86 boot sector, extended partition table
  358. /dev/hda4: Linux/i386 ext2 filesystem
  359. /dev/hda5: Linux/i386 swap file
  360. /dev/hda6: Linux/i386 swap file
  361. /dev/hda7: Linux/i386 swap file
  362. /dev/hda8: Linux/i386 swap file
  363. /dev/hda9: empty
  364. /dev/hda10: empty
  365. $ file -i file.c file /dev/{wd0a,hda}
  366. file.c: text/x-c
  367. file: application/x-executable, dynamically linked (uses shared libs),
  368. not stripped
  369. /dev/hda: application/x-not-regular-file
  370. /dev/wd0a: application/x-not-regular-file
  371. .fi
  372. .SH HISTORY
  373. There has been a
  374. .B file
  375. command in every \s-1UNIX\s0 since at least Research Version 4
  376. (man page dated November, 1973).
  377. The System V version introduced one significant major change:
  378. the external list of magic number types.
  379. This slowed the program down slightly but made it a lot more flexible.
  380. .PP
  381. This program, based on the System V version,
  382. was written by Ian Darwin <ian@darwinsys.com>
  383. without looking at anybody else's source code.
  384. .PP
  385. John Gilmore revised the code extensively, making it better than
  386. the first version.
  387. Geoff Collyer found several inadequacies
  388. and provided some magic file entries.
  389. Contributions by the `&' operator by Rob McMahon, cudcv@warwick.ac.uk, 1989.
  390. .PP
  391. Guy Harris, guy@netapp.com, made many changes from 1993 to the present.
  392. .PP
  393. Primary development and maintenance from 1990 to the present by
  394. Christos Zoulas (christos@astron.com).
  395. .PP
  396. Altered by Chris Lowth, chris@lowth.com, 2000:
  397. Handle the ``-i'' option to output mime type strings and using an alternative
  398. magic file and internal logic.
  399. .PP
  400. Altered by Eric Fischer (enf@pobox.com), July, 2000,
  401. to identify character codes and attempt to identify the languages
  402. of non-ASCII files.
  403. .PP
  404. The list of contributors to the "Magdir" directory (source for the
  405. /etc/magic
  406. file) is too long to include here.
  407. You know who you are; thank you.
  408. .SH LEGAL NOTICE
  409. Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
  410. Covered by the standard Berkeley Software Distribution copyright; see the file
  411. LEGAL.NOTICE in the source distribution.
  412. .PP
  413. The files
  414. .I tar.h
  415. and
  416. .I is_tar.c
  417. were written by John Gilmore from his public-domain
  418. .B tar
  419. program, and are not covered by the above license.
  420. .SH BUGS
  421. There must be a better way to automate the construction of the Magic
  422. file from all the glop in magdir.
  423. What is it?
  424. Better yet, the magic file should be compiled into binary (say,
  425. .BR ndbm (3)
  426. or, better yet, fixed-length
  427. .SM ASCII
  428. strings for use in heterogenous network environments) for faster startup.
  429. Then the program would run as fast as the Version 7 program of the same name,
  430. with the flexibility of the System V version.
  431. .PP
  432. .B File
  433. uses several algorithms that favor speed over accuracy,
  434. thus it can be misled about the contents of
  435. text
  436. files.
  437. .PP
  438. The support for
  439. text
  440. files (primarily for programming languages)
  441. is simplistic, inefficient and requires recompilation to update.
  442. .PP
  443. There should be an ``else'' clause to follow a series of continuation lines.
  444. .PP
  445. The magic file and keywords should have regular expression support.
  446. Their use of
  447. .SM "ASCII TAB"
  448. as a field delimiter is ugly and makes
  449. it hard to edit the files, but is entrenched.
  450. .PP
  451. It might be advisable to allow upper-case letters in keywords
  452. for e.g.,
  453. .BR troff (1)
  454. commands vs man page macros.
  455. Regular expression support would make this easy.
  456. .PP
  457. The program doesn't grok \s-2FORTRAN\s0.
  458. It should be able to figure \s-2FORTRAN\s0 by seeing some keywords which
  459. appear indented at the start of line.
  460. Regular expression support would make this easy.
  461. .PP
  462. The list of keywords in
  463. .I ascmagic
  464. probably belongs in the Magic file.
  465. This could be done by using some keyword like `*' for the offset value.
  466. .PP
  467. Another optimisation would be to sort
  468. the magic file so that we can just run down all the
  469. tests for the first byte, first word, first long, etc, once we
  470. have fetched it.
  471. Complain about conflicts in the magic file entries.
  472. Make a rule that the magic entries sort based on file offset rather
  473. than position within the magic file?
  474. .PP
  475. The program should provide a way to give an estimate
  476. of ``how good'' a guess is.
  477. We end up removing guesses (e.g. ``From '' as first 5 chars of file) because
  478. they are not as good as other guesses (e.g. ``Newsgroups:'' versus
  479. ``Return-Path:'').
  480. Still, if the others don't pan out, it should be possible to use the
  481. first guess.
  482. .PP
  483. This program is slower than some vendors' file commands.
  484. The new support for multiple character codes makes it even slower.
  485. .PP
  486. This manual page, and particularly this section, is too long.
  487. .SH RETURN CODE
  488. .B file
  489. almost always returns 0. It returns a different if it cannot open a file.
  490. .SH AVAILABILITY
  491. You can obtain the original author's latest version by anonymous FTP
  492. on
  493. .B ftp.astron.com
  494. in the directory
  495. .I /pub/file/file-X.YZ.tar.gz
  496. .PP
  497. This
  498. .B Debian
  499. version adds a number of new magix entries. It can be
  500. obtained from every site carrying a
  501. .B Debian
  502. distribution (ftp.debian.org and mirrors).