Linux命令详解-file file命令用来识别文件类型,也可用来辨别一些文件的编码格式。它是通过查看文件的头部信息来获取文件类型,而不是像Windows通过扩展名来确定文件类型的。 1.命令格式: file [ -bchikLnNprsvz ] [ -f namefile ] [ -F separator ] [ -m magicfiles ] file ... 2.命令功能: 用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份 3.命令参数: -b 列出文件辨识结果时,不显示文件名称。 -c 详细显示指令执行过程,便于排错或分析程序执行的情形 -f 列出文件中文件名的文件类型 -F 使用指定分隔符号替换输出文件名后的默认的":"分隔符。 -i 输出mime类型的字符串 -L 查看对应软链接对应文件的文件类型 -z 尝试去解读压缩文件的内容 --help 显示命令在线帮助 -version 显示命令版本信息 4.使用实例: 实例1:查看文件类型 命令: file Temp.txt 输出: [root@test0 ~]# file anaconda-ks.cfg anaconda-ks.cfg: ASCII text 实例2:不输出文件名称,只显示文件格式以及编码 命令: file –b Temp.txt 输出: [root@test0 ~]# file -b anaconda-ks.cfg ASCII text 实例3:输出mime类型的字符串 命令: file –i Temp.txt 输出: [root@test0 ~]# file -i anaconda-ks.cfg anaconda-ks.cfg: text/plain; charset=us-ascii 实例3:查看文件中的文件名的文件类型 命令: file -f test 输出: [root@test0 ~]# cat txt /etc/profile /etc/networks /etc/rc.d it`s a test [root@test0 ~]# file -f txt /etc/profile: UTF-8 Unicode text /etc/networks: ASCII text /etc/rc.d: directory it`s a test: cannot open (No such file or directory) 实例4:使用指定分隔符号替换输出文件名后的默认的":"分隔符。 命令: file –F "--" test 输出: [root@test0 ~]# file -F '--' txt txt-- ASCII text 实例5:尝试去解读压缩文件的内容 命令: file –z test.gz 输出: [root@test0 ~]# file -z tar/nginx-1.14.2.tar.gz tar/nginx-1.14.2.tar.gz: POSIX tar archive (gzip compressed data, from Unix, last modified: Tue Dec 4 22:52:32 2018) 实例6:查看软链接对应文件的文件类型 命令: file -L sfile 输出: [root@test0 txtt]# ll total 8 -rw-------. 2 root root 1207 May 11 04:00 anaconda-ks.cfg lrwxrwxrwx 1 root root 8 Jul 5 10:56 test.txt -> test.txt -rw-r--r-- 1 root root 49 Jul 5 10:53 txt [root@test0 txtt]# file -L ~/txtt/* /root/txtt/anaconda-ks.cfg: ASCII text /root/txtt/test.txt: cannot open (Too many levels of symbolic links) /root/txtt/txt: ASCII text [root@test0 txtt]# cd .. [root@test0 ~]# file -L test.txt test.txt: UTF-8 Unicode text [root@test0 ~]# file -L anaconda-ks.cfg anaconda-ks.cfg: ASCII text