也想出现在这里? 联系我们

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

作者 : 小编 本文共3002个字,预计阅读时间需要8分钟 发布时间: 2022-11-1 共4.01K人阅读
也想出现在这里? 联系我们

命令简介:

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

执行权限 :All User

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

命令语法:

复制代码

代码如下:

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

命令参数:

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

使用示例: 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:不输出文件名称,只显示文件格式以及编码通过下面两个命令对时,就可以清晰的了解参数-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 $ dirBACKUP master.zipcrossroads-stable.tar.gz num.txtEDWARD-MAYA-2011-2012-NEW-REMIX.mp3 reggea.xspfLinux-Security-Optimization-Book.gif tmp-linktecmint@tecmint ~/Linux-Tricks $ file BACKUP/BACKUP/: directory tecmint@tecmint ~/Linux-Tricks $ file master.zip master.zip: Zip archive data, at least v1.0 to extracttecmint@tecmint ~/Linux-Tricks $ file crossroads-stable.tar.gzcrossroads-stable.tar.gz: gzip compressed data, from Unix, last modified: Tue Apr 5 15:15:20 2011tecmint@tecmint ~/Linux-Tricks $ file Linux-Security-Optimization-Book.gif Linux-Security-Optimization-Book.gif: GIF image data, version 89a, 200 x 259tecmint@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, JntStereotecmint@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 ~]#

1. 本站所提供的源码模板(主题/插件)等资源仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,有部分资源为网上收集或仿制而来,若模板侵犯了您的合法权益,请来信通知我们(Email: rayer@88.com),我们会及时删除,给您带来的不便,我们深表歉意!
2. 分享目的仅供大家学习和交流,请不要用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布投稿,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务 请大家谅解!
5. 如有链接无法下载、失效或广告,请联系站长,可领回失去的金币,并额外有奖!
6. 如遇到加密压缩包,默认解压密码为"www.zyfx8.cn",如遇到无法解压的请联系管理员!
本站部分文章、资源来自互联网,版权归原作者及网站所有,如果侵犯了您的权利,请及时联系我站删除。免责声明
资源分享吧 » Linux系统中file命令的使用详解

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
织梦模板使用说明
你下载的织梦模板并不包括DedeCMS使用授权,根据DedeCMS授权协议,除个人非盈利站点外,均需购买DedeCMS商业使用授权。购买地址: http://www.desdev.cn/service-dedecms.html

发表评论

Copyright 2015-2020 版权所有 资源分享吧 Rights Reserved. 蜀ICP备14022927号-1
开通VIP 享更多特权,建议使用QQ登录