file 官方解释:Determine type of FILEs. 判定文件的类型。 

小知识:在UNIX/Linux系统中,文件类型并不是由扩展名决定的(Windows是的)。 

开篇例子:

1. $file file.txt

输出:file.txt: ASCII English text

注:file.txt是我用file –help > file.txt生成的帮助文档 

查看—help

Usage: file [OPTION...] [FILE...] 
Determine type of FILEs.

--help   display this help and exit 
-v, --version   output version information and exit 
-m, --magic-file  LIST  use LIST as a colon-separated list of magic 
number files 
-z, --uncompress   try to look inside compressed files 
-b, --brief    do not prepend filenames to output lines 
-c, --checking-printout   print the parsed form of the magic file, use in 
conjunction with -m to debug a new magic file before installing it 
-e, --exclude TEST   exclude TEST from the list of test to be performed for file. Valid tests are: ascii, apptype, compress, elf, soft, tar, tokens, troff 
-f, --files-from FILE   read the filenames to be examined from FILE 
-F, --separator STRING   use string as separator instead of `:' 
-i, --mime  output MIME type strings (--mime-type and 
--mime-encoding) 
--apple  output the Apple CREATOR/TYPE 
--mime-type  output the MIME type 
--mime-encoding  output the MIME encoding 
-k, --keep-going  don't stop at the first match 
-l, --list  list magic strength 
-L, --dereference  follow symlinks (default) 
-h, --no-dereference  don't follow symlinks 
-n, --no-buffer  do not buffer output 
-N, --no-pad  do not pad output 
-0, --print0  terminate filenames with ASCII NUL 
-p, --preserve-date  preserve access times on files 
-r, --raw  don't translate unprintable chars to \ooo 
-s, --special-files  treat special (block/char devices) files as ordinary ones 
-C, --compile  compile file specified by -m 
-d, --debug  print debugging messages

 

解读: 

1. -z

查看压缩文件内的信息

2. -b, -e, -i, –r

-b 去掉输出行的前缀命名

-e 从测试列表到被执行文件排除测试。有效的测试包括:ascii, apptype, compress, elf, soft, tar,

-i 打印MIME

-R 不转译无法打印的字符为\ooo

3. magic file

以mgc为扩展名的文件,Microsoft office媒体目录文件

 

实战实例:

1.

 
    
  1. $file -z file.zip 

查看file.zip包中的文件信息

2.

 
    
  1. $file –i file.txt 

out:file.txt: text/plain; charset=us-ascii

打印MIME类型

 

 
    
  1. $file file.txt 

out:file.txt: ASCII English text

3. magic file

我翻译起来实在蹩脚。。 只能猜测查看$which file 看看,没有收获。最终通过$file得到

 

Usage: file  [-bchikLlNnprsvz0] [--apple] [--mime-encoding] [--mime-type] 
      [-e testname] [-F separator] [-f namefile] [-m magicfiles] file ... 
file  -C [-m magicfiles] 
file  [--help]

然后尝试file –C 得到magic.mgc文件。。。 不知有何用,惭愧

 有点尴尬的翻译了一下 见附件。