file 用于查看文件类型。Linux 系统与 Windows 系统不同,不通过文件名来反映文件类型,因此需要使用 file 命令来确认。
[admin@local ~]$ file exe
exe: Bourne-Again shell script text executable
如上,使用 file 命令查看 exe 文件的类型,显示为可执行的 bash 脚本文件。
常用选项
-i:以 MIME 类型显示文件信息
[admin@local ~]$ file -i exe
exe: text/x-shellscript; charset=us-ascii
-L:显示软链接所对应文件的类型
[admin@local ~]$ file ln
ln: symbolic link to `exe'
[admin@local ~]$ file -L ln
ln: Bourne-Again shell script text executable
-f:依次将文件中每行内容作为参数执行 file 命令
[admin@local ~]$ vi text
dir
exe
ln
pipe
/etc
etc
[admin@local ~]$ file -f text
dir: directory
exe: Bourne-Again shell script text executable
ln: symbolic link to `exe'
pipe: fifo (named pipe)
/etc: directory
etc: cannot open `etc' (No such file or directory)
如上,将 text 作为 file -f 命令的参数,输出为对其每行内容执行 file 结果。
注:如文件中内容非绝对路径,则在文件所在目录下进行查找
-z:尝试查看压缩文件中内容
[admin@local ~]$ zip text.zip text
adding: text (deflated 4%)
[admin@local ~]$ file -z text.zip
text.zip: ASCII text (Zip archive data, at least v2.0 to extract)
注 1:若压缩文件中有多个文件,则只显示首个文件的信息
[admin@local ~]$ zip cmp.zip exe text
adding: exe (deflated 29%)
adding: text (deflated 4%)
[admin@local ~]$ file -z cmp.zip
cmp.zip: Bourne-Again shell script text executable (Zip archive data, at least v2.0 to extract)
注 2:此命令无法查看 tar 命令打包压缩的文件中内容
[admin@local ~]$ tar zcf text.tar.gz text
[admin@local ~]$ file -z text.tar.gz
text.tar.gz: POSIX tar archive (GNU) (gzip compressed data, from Unix, last modified: Wed Jan 24 15:44:41 2018)
以下为使用 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, --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