一天一个shell命令 linux文本系列-file命令用法

所属分类: 脚本专栏 / linux shell 阅读数: 632
收藏 0 赞 0 分享

linux file 命令简介:

该命令用来识别文件类型,也可用来辨别一些文件的编码格式。它是通过查看文件的头部信息来获取文件类型,而不是像Windows通过扩展名来确定文件类型的。

更多介绍可以查看这篇文章:https://www.jb51.net/LINUXjishu/388984.html

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. $file -z file.zip
查看file.zip包中的文件信息

2. $file –i file.txt
out:file.txt: text/plain; charset=us-ascii

打印MIME类型
 
$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文件。。。 不知有何用,惭愧

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       以冒号分割的魔法数字清单作为列表
                           
  -z, --uncompress           查看压缩文件内容
  -b, --brief                不前缀文件名到输出行里
  -c, --checking-printout    打印魔法文件的解析格式,安装前,用-m debug一个新的魔法文件
                             
  -e, --exclude TEST         排除测试从测试列表到执行文件。有效的测试:ascii, apptype, compress, elf, soft, tar, tokens, troff
  -f, --files-from FILE      从已查的文档中读取文件名
  -F, --separator STRING     用String作为分隔器 代替 “:”
  -i, --mime                 输出MIME类型的字符串 (--mime-type and
                               --mime-encoding)
         
      --apple                输出苹果类型 CREATOR/TYPE
      --mime-type            打印MIME类型
      --mime-encoding        打印 MIME 编码
  -k, --keep-going           不停留在第一个匹配
  -l, --list                 列出magic深度
  -L, --dereference          跟随符号链接(缺省)
  -h, --no-dereference       不跟随符号链接
  -n, --no-buffer            不缓冲输出
  -N, --no-pad               不打印输出
  -0, --print0               NUL终止文件
  -p, --preserve-date        保持文件上的原访问时间
  -r, --raw                  不转移不可打印的字符到\ooo
  -s, --special-files        把特殊(块/字符 设备)文件作为普通文件对待
  -C, --compile              编译用-m指定的文件
  -d, --debug                打印调试信息

Report bugs to http://bugs.gw.com/

更多精彩内容其他人还在看

linux创建用户useradd命令代码示例

本文通过代码示例给大家介绍了adduser与useradd命令二者的关系 以及使用useradd命令添加用户的方法,需要的朋友参考下吧
收藏 0 赞 0 分享

Linux 中LVS NAT 配置步骤的详解

这篇文章主要介绍了Linux 中LVS NAT 配置步骤的详解的相关资料,这里列出详细的实现步骤,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux中的内核链表实例详解

这篇文章主要介绍了Linux中的内核链表实例详解的相关资料,链表中一般都要进行初始化、插入、删除、显示、释放链表,寻找节点这几个操作,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell中特殊字符的用法总结大全

这篇文章主要给大家总结了关于Shell中特殊字符的相关资料,文中包括分好、&、#、!、$、大于号、单双引号等等一系列特殊字符的用法,通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Shell脚步攻略之管道重定向基础

管道是为了解决进程间通信问题而存在,它可以让两个进程之间的数据进行传递,将一个进程的输出数据传递给另一个进程作为其输入数据
收藏 0 赞 0 分享

Linux shell数组循环的实例详解

这篇文章主要介绍了Linux shell数组循环的实例详解的相关资料,这里举例说明如何实现shell数组循环,需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell内置判断语句

内置判断,成功的时候返回0,不成功返回非零。接下来通过本文重点给大家介绍linux shell内置判断语句,感兴趣的的朋友一起看看吧
收藏 0 赞 0 分享

Linux文件的归档和压缩命令

文件归档命令tar,文件归档有好多好处,方便使用易于管理,接下来通过本文给大家分享linux文件的归档和压缩命令,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Linux Shell中curl和wget使用代理IP的方法教程

这篇文章主要给大家介绍了关于在Linux Shell中curl和wget使用代理IP的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
收藏 0 赞 0 分享

Linux 日常常用指令及应用小结

最近在学习一些基本的Linux指令,在这里总结一下,在搭环境中常用的一些指令,熟悉这些指令就基本能够使用CentOS进行日常操作了
收藏 0 赞 0 分享
查看更多