Linux命令学习总结: file命令

命令简介:

该命令用来识别文件类型,也可用来辨别一些文件的编码格式。它是通过查看文件的头部信息来获取文件类型,而不是像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 ~]# 
时间: 2024-09-18 07:35:43

Linux命令学习总结: file命令的相关文章

图解Linux命令:用file命令验明文件正身

file 命令的使用方法非常简单.格式为:file <文件路径> <文件名>. 现在终端返回的信息是说这是文本文件.不过这个命令能支持各种文件,现在看看可执行文件返回什么信息. 哇!这回信息可真详细啊.说是 32 位的可执行文件.支持处理器 Intel 80386.要求 Linux 内核版本 2.2.5.是一个拥有图形界面的程序喔!现在来看看图片文件会返回什么信息呢? 原来说是 http://www.aliyun.com/zixun/aggregation/16701.html&q

Linux 命令学习之dpkg命令详解

dpkg是一个Debian的一个命令行工具,它可以用来安装.删除.构建和管理Debian的软件包. 下面是它的一些命令解释: 1)安装软件 命令行:dpkg -i <.deb file name> 示例:dpkg -i avg71flm_r28-1_i386.deb 2)安装一个目录下面所有的软件包 命令行:dpkg -R 示例:dpkg -R /usr/local/src 3)释放软件包,但是不进行配置 命令行:dpkg –unpack package_file 如果和-R一起使用,参数可以

c/c++unix/linux基础学习笔记-常用命令和vi的使用

linux 基本命令的使用-命令在ubuntu下面执行,有些命令通用其他linux,有些不通用. 多条命令间用;号隔开,回车后可以一起执行. clear-前屏,pwd显示当前目录,cd跳转目录. sudo [命令]  -ubuntu 下以管理员身份运行命令. 一般情况下,运行当前目录下的程序,要用 ./文件名 执行. 查看当前shell名称:ps 进入另外一个shell,直接输入shell名称:ksh/tcsh/sh/bash,退出一个shell用:exit. 切换shell命令,如:exec

【AIX 命令学习】mkvg 命令

用途 创建卷组. 语法 mkvg [ -B ] [ -t factor ] [ -S [ -v LogicalVolumes ] [ -P Partitions ] ] [ -C ] [ -G ] [ -f ] [ -i ] [ -I ][ -c] [ -x ] [ -L LTGSize ] [ -n ] [ -s Size ] [ -V MajorNumber ] [ -y VolumeGroup ] PhysicalVolume ... 描述 mkvg 命令使用 PhysicalVolume

【AIX 命令学习】migratepv 命令

用途 将已分配的物理分区从一个物理卷移动到另一个或多个其他物理卷. 语法migratepv [  -i ] [  -l LogicalVolume ] SourcePhysicalVolume DestinationPhysicalVolume... 描述   migratepv 命令将已分配的物理分区和它们包含的数据从 SourcePhysicalVolume 移到一个或多个其他物理分区.要限制传送到特定的物理卷,请在 DestinationPhysicalVolume 参数中使用一个或多个物

【AIX 命令学习】lsvg 命令参数使用!

ufserver[/]#lsvg rootvg  ----查看rootvg卷组的详细信息及状态 VOLUME GROUP:       rootvg                   VG IDENTIFIER:  00cecd3e00004c000000011e4872bbdd VG STATE:           active                   PP SIZE:        128 megabyte(s) VG PERMISSION:      read/write 

【AIX 命令学习】errpt 命令参数使用!

el_sequence 错误记录戳记编号 el_label 错误标签 el_timestamp 错误日志记录的时间戳记 el_crcid 唯一的循环冗余检验(CRC)的错误标识符 el_machineid 机器标识变量 el_nodeid 节点标识变量 el_class 错误类 el_type 错误类型 el_resource 资源名称 el_rclass 资源类 el_rtype 资源类型 el_vpd_ibm IBM 重要产品数据(VPD) el_vpd_user 用户 VPD el_in

【AIX 命令学习】errpt 命令示例!

要想显示一个完整的摘要报告,请输入: errpt 要想显示一个完整的详细报告,请输入: errpt  -a 要想显示一个错误标识符 E19E094F 记录的所有的错误的报表,请输入: errpt  -a  -j E19E094F 要想显示一个在过去 24 小时内记录下来的所有错误的详细报表,请输入: errpt  -a  -s mmddhhmmyy 这里,mmddhhmmyy 字符串等于当前的月.日.小时.分和年再减去 24 小时. 要想列出为其日志为任何错误日志记录关闭的错误记录模板,请输入:

Linux命令学习总结之rm命令_Linux

命令简介: rm命令用来删除Linux系统中的文件或目录.通常情况下rm不会删除目录,你必须通过指定参数-r或-R来删除目录.另外rm通常可以将该文件或目录恢复(注意,rm删除文件其实只是将指向数据块的索引点(information nodes)释放,只要不被覆盖,数据其实还在硬盘上.如果想要保证文件的内容无法复原,可以使用命令shred . 另外一般还是要慎用rm -rf *这样的命令. 执行权限 :All User 指令所在路径:/bin/rm 命令语法: rm [OPTION]... FI