file命令

作者: 飞奔的大虎 | 来源:发表于2021-07-26 16:41 被阅读0次

命令简介: 

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

执行权限:All User 

指令所在路径:/usr/bin/file 

命令语法: 

file [ -bchikLnNprsvz ] [ -f namefile ] [ -F separator ] [ -m magicfiles ] file ... 

命令参数: 

下表列出了部分常用的参数。 

参数长参数描叙

-b 列出文件辨识结果时,不显示文件名称。

-c 详细显示指令执行过程,便于排错或分析程序执行的情形

-f 列出文件中文件名的文件类型

-F 使用指定分隔符号替换输出文件名后的默认的“:”分隔符。

-i 输出mime类型的字符串

-L 查看对应软链接对应文件的文件类型

-z 尝试去解读压缩文件的内容

 --help显示命令在线帮助

 --version显示命令版本信息

使用示例: 

1:查看file命令的帮助信息

[root@DB-Server ~]# file --help

Usage: file [OPTION]... [FILE]...

Determine file type of FILEs.

  -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

  -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

  -k, --keep-going           don't stop at the first match

  -L, --dereference          causes symlinks to be followed

  -n, --no-buffer            do not buffer output

  -N, --no-pad               do not pad output

  -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

      --help                 display this help and exit

      --version              output version information and exit

当然你也可以使用man file 获取更加详细的帮助文档信息。 

2:查看文件类型 

例如,如下所示,Temp.txt 文件类型为text,编码为UTF-8 Unicode

[root@DB-Server ~]# file Temp.txt

Temp.txt: UTF-8 Unicode text, with very long lines, with CRLF line terminators

3:不输出文件名称,只显示文件格式以及编码 

通过下面两个命令对时,就可以清晰的了解参数-b的作用。

[root@DB-Server ~]# file Temp.txt

Temp.txt: UTF-8 Unicode text, with very long lines, with CRLF line terminators

[root@DB-Server ~]# file -b Temp.txt

UTF-8 Unicode text, with very long lines, with CRLF line terminators

4: 输出mime类型的字符串

[root@DB-Server ~]# file -i Temp.txt

Temp.txt: text/plain; charset=utf-8

5: 查看文件中的文件名的文件类型 

这个参数非常适合shell脚本去查找、判别某种文件类型的数据。

[root@DB-Server ~]# cat >test

/root/install.log

it is only one test file

[2]+  Stopped                 cat > test

[root@DB-Server ~]# file -f  test

/root/install.log:        ASCII text

it is only one test file: ERROR: cannot open `it is only one test file' (No such file or directory)

[root@DB-Server ~]#

[root@DB-Server ~]#

5: 使用指定分隔符号替换输出文件名后的默认的“:”分隔符。 

感觉这个参数很鸡肋!我搞明白这个参数的作用时,很是纳闷。 

6:尝试去解读压缩文件的内容

[root@DB-Server ~]# file -z Temp.txt.gz

Temp.txt.gz: UTF-8 Unicode text, with very long lines, with CRLF line terminators (gzip compressed data, was "Temp.txt", from Unix, last modified: Tue Jun 24 00:34:15 2014)

[root@DB-Server ~]#

7: 查看软链接对应文件的文件类型 

如下所示,创建一个软链接sfile,然后分别用file 和带参数的file -L查看 

[root@DB-Server ~]# ln -s Temp.txt.gz sfile

[root@DB-Server ~]# file sfile

sfile: symbolic link to `Temp.txt.gz'

[root@DB-Server ~]# file -L sfile

sfile: gzip compressed data, was "Temp.txt", from Unix, last modified: Tue Jun 24 00:34:15 2014

[root@DB-Server ~]#

作者:潇湘隐者

出处:http://www.cnblogs.com/kerrycode/

相关文章

  • docker-compose命令

    帮助文档 命令解析 docker-compose 命令选项[-f ...] -f, --file FILE 指定...

  • Linux命令之文件管理 (十)

    Linux file命令 Linux file命令用于辨识文件类型。通过file指令,我们得以辨识该文件的类型。 ...

  • file命令

    命令简介: 该命令用来识别文件类型,也可用来辨别一些文件的编码格式。它是通过查看文件的头部信息来获取文件类型,而不...

  • 静态库cpu架构查看

    使用lipo -info 命令 使用file命令

  • fuser和find命令

    fuser命令 fuser命令是用来显示所有正在使用着指定file, file system, socket的进程...

  • Linux 其他常用命令

    SCP远程文件拷贝命令scp [可选参数] file_source file_target-1: 强制scp命令使...

  • Git笔记

    命令 PS: 以下均可替换为 . / * / 初始化及配置 文件 / 目录...

  • file命令,find命令

    (1)file命令 file 命令用于查看文件的类型。在 Linux 中,有些文件的后缀没法看出来它是什么文件类型...

  • grep命令用法

    一、 grep 搜索文本的命令 命令格式:grep[options]... pattern [file]...命令...

  • Linux 重定向 Shell 输出

    重定向命令 命令说明command > file输出重定向到文件command < file输入重定向到文件com...

网友评论

    本文标题:file命令

    本文链接:https://www.haomeiwen.com/subject/ajdgmltx.html