您的位置:首页 > 运维架构 > Linux

Linux命令学习总结: file命令

2014-06-24 17:59 274 查看
命令简介:该命令用来识别文件类型,也可用来辨别一些文件的编码格式。它是通过查看文件的头部信息来获取文件类型,而不是像Windows通过扩展名来确定文件类型的。执行权限:AllUser指令所在路径:/usr/bin/file命令语法:file[-bchikLnNprsvz][-fnamefile][-Fseparator][-mmagicfiles]file...命令参数:下表列出了部分常用的参数。

参数

长参数

描叙

-b

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

-c

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

-f

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

-F

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

-i

输出mime类型的字符串

-L

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

-z

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

--help

显示命令在线帮助

--version

显示命令版本信息

使用示例:

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

[root@DB-Server~]#file--help
Usage:file[OPTION]...[FILE]...
DeterminefiletypeofFILEs.
-m,--magic-fileLISTuseLISTasacolon-separatedlistofmagic
numberfiles
-z,--uncompresstrytolookinsidecompressedfiles
-b,--briefdonotprependfilenamestooutputlines
-c,--checking-printoutprinttheparsedformofthemagicfile,usein
conjunctionwith-mtodebuganewmagicfile
beforeinstallingit
-f,--files-fromFILEreadthefilenamestobeexaminedfromFILE
-F,--separatorstringusestringasseparatorinsteadof`:'
-i,--mimeoutputmimetypestrings
-k,--keep-goingdon'tstopatthefirstmatch
-L,--dereferencecausessymlinkstobefollowed
-n,--no-bufferdonotbufferoutput
-N,--no-paddonotpadoutput
-p,--preserve-datepreserveaccesstimesonfiles
-r,--rawdon'ttranslateunprintablecharsto\ooo
-s,--special-filestreatspecial(block/chardevices)filesas
ordinaryones
--helpdisplaythishelpandexit
--versionoutputversioninformationandexit

当然你也可以使用manfile获取更加详细的帮助文档信息。
2:查看文件类型
例如,如下所示,Temp.txt文件类型为text,编码为UTF-8Unicode

[root@DB-Server~]#fileTemp.txt
Temp.txt:UTF-8Unicodetext,withverylonglines,withCRLFlineterminators

3:不输出文件名称,只显示文件格式以及编码
通过下面两个命令对时,就可以清晰的了解参数-b的作用。

[root@DB-Server~]#fileTemp.txt
Temp.txt:UTF-8Unicodetext,withverylonglines,withCRLFlineterminators
[root@DB-Server~]#file-bTemp.txt
UTF-8Unicodetext,withverylonglines,withCRLFlineterminators




4:输出mime类型的字符串

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

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

[root@DB-Server~]#cat>test
/root/install.log
itisonlyonetestfile
[2]+Stoppedcat>test
[root@DB-Server~]#file-ftest
/root/install.log:ASCIItext
itisonlyonetestfile:ERROR:cannotopen`itisonlyonetestfile'(Nosuchfileordirectory)
[root@DB-Server~]#
[root@DB-Server~]#




5:使用指定分隔符号替换输出文件名后的默认的“:”分隔符。
感觉这个参数很鸡肋!我搞明白这个参数的作用时,很是纳闷。



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

[root@DB-Server~]#file-zTemp.txt.gz
Temp.txt.gz:UTF-8Unicodetext,withverylonglines,withCRLFlineterminators(gzipcompresseddata,was"Temp.txt",fromUnix,lastmodified:TueJun2400:34:152014)
[root@DB-Server~]#

7:查看软链接对应文件的文件类型
如下所示,创建一个软链接sfile,然后分别用file和带参数的file-L查看

[root@DB-Server~]#ln-sTemp.txt.gzsfile
[root@DB-Server~]#filesfile
sfile:symboliclinkto`Temp.txt.gz'
[root@DB-Server~]#file-Lsfile
sfile:gzipcompresseddata,was"Temp.txt",fromUnix,lastmodified:TueJun2400:34:152014
[root@DB-Server~]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: