file.man 16 KB

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