Linux系统中file命令的使用详解

2016-03-31 15:16 16 1 收藏

今天图老师小编要向大家分享个Linux系统中file命令的使用详解教程,过程简单易学,相信聪明的你一定能轻松get!

【 tulaoshi.com - 服务器 】

Linux系统中file命令的使用详解

   命令简介:

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

  执行权限 :All User

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

  命令语法:

  代码如下:

  file [ -bchikLnNprsvz ] [ -f namefile ] [ -F separator ] [ -m magicfiles ] 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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/fuwuqi/)

  -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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/fuwuqi/)

  -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:不输出文件名称,只显示文件格式以及编码

  通过下面两个命令对时,就可以清晰的了解参数-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.txtUTF-8 Unicode text, with very long lines, with CRLF line terminators

  3: 输出mime类型的字符串

  代码如下:

  [root@DB-Server ~]# file -i Temp.txt Temp.txt: text/plain; charset=utf-8

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

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

  你可以像下面这样使用 file 命令确定文件的类型。下面的截图显示了用 file 命令确定不同文件类型的例子。

  代码如下:

  tecmint@tecmint ~/Linux-Tricks $ dir

  BACKUP master.zip

  crossroads-stable.tar.gz num.txt

  EDWARD-MAYA-2011-2012-NEW-REMIX.mp3 reggea.xspf

  Linux-Security-Optimization-Book.gif tmp-link

  tecmint@tecmint ~/Linux-Tricks $ file BACKUP/

  BACKUP/: directory

  tecmint@tecmint ~/Linux-Tricks $ file master.zip

  master.zip: Zip archive data, at least v1.0 to extract

  tecmint@tecmint ~/Linux-Tricks $ file crossroads-stable.tar.gz

  crossroads-stable.tar.gz: gzip compressed data, from Unix, last modified: Tue Apr 5 15:15:20 2011

  tecmint@tecmint ~/Linux-Tricks $ file Linux-Security-Optimization-Book.gif

  Linux-Security-Optimization-Book.gif: GIF image data, version 89a, 200 x 259

  tecmint@tecmint ~/Linux-Tricks $ file EDWARD-MAYA-2011-2012-NEW-REMIX.mp3

  EDWARD-MAYA-2011-2012-NEW-REMIX.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, JntStereo

  tecmint@tecmint ~/Linux-Tricks $ file /dev/sda1

  /dev/sda1: block special

  tecmint@tecmint ~/Linux-Tricks $ file /dev/tty1

  /dev/tty1: character special

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

  代码如下:

  [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 ~]#

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

  如下所示,创建一个软链接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 ~]#

来源:https://www.tulaoshi.com/n/20160331/2050297.html

延伸阅读
标签: 服务器
Linux中以sar命令监控系统的使用教程   sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告, 包括:文件的读写情况、系统调用的使用情况、磁盘I/O、CPU效率、内存使用状况、进程活动及IPC有关的活动等。 本文主要以CentOS 6.3 x64系统为例...
标签: 电脑入门
Linux系统操作中,由于各种原因而导致某些程序在运行的时候断开,这就需要用到Linux中的nohup命令了,使用该命令可以让程序一直在后台中运行,一起来学习下吧。 在linux操作系统中从后台一直运行某个程序的方法,就是使用nohup命令了。 Unix/Linux下一般比如想让某个程序在后台运行,很多都是使用 & 在程序结尾来让程序自动运行。 ...
标签: 服务器
Linux中cron命令的用法详解   linux中有一个命令可以定期来执行系统任务。这就是crond服务。下面介绍下crontab命令的用法。 linux任务调度的工作主要分为以下两类: 编辑/etc/crontab 文件配置cron cron服务每分钟不仅要读一次 /var/spool/cron内的所有文件,还需要读一次/etc/crontab,因此我们配置这个文件也能运用...
标签: 电脑入门
在Linux系统操作过程中,没有扩展名的文件难以识别,我们可以使用命令来识别文件类型,下面图老师小编就给大家介绍下如何使用file命令来识别文件类型。 我们知道Linux文件系统是Linux内核支持的基本功能之一,并由根 / 开始往下形成倒立树状的级连式结构。文件与目录是Linux文件系统的两个基本元素,(广义的)文件分为普通文件、目录文件...
标签: 电脑入门
Linux中ldd命令主要用于查看程式运行所需的共享库,那么ldd命令具体要如何使用呢?下面图老师小编就给大家介绍下Linux下ldd命令的使用方法,感兴趣的朋友一起来学习下吧。 ldd命令用于判断某个可执行的 binary 档案含有什么动态函式库 Linux Ldd参数说明: --version打印ldd的版本号 -v --verbose打印所有信息,例如包括符号的版本信...

经验教程

926

收藏

56
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部