如何判断任意文件类型
1问题
从客户那里拿来的数据文件要入库,不知道是哪种数据库类型,也不知道是哪种备份方式。
进一步没有后缀的文件,如何判断文件类型并使用相对应的工具并解析之?
2可用的方案
不能简单的通过后缀名来判断文件的类型
一方面文件后缀名可以随意修改或者删除。
另一方面在Linux上一切对象皆文件,完全不需要后缀名。
https://www.zhihu.com/question/422144033
很多文件在文件头都会标识这种文件的类型,这几个字节也被称为"魔数"。
“魔数"是一个数字,通常使用十六进制表示。
你可以通过如下命令查看指定文件的"魔数”。
hexdump -C 文件路径 | less
2.1 使用HEX编辑工具
EmEditor
https://zh-cn.emeditor.com/text-editor-features/more-features/binary-editing/
Ultraedit
https://www.ultraedit.com/language/hex-editor.html
WinHex
https://www.x-ways.net/winhex/
DiskGenius
https://www.diskgenius.cn
010Editor
http://www.sweetscape.com/010editor/
notepad++
https://notepad-plus-plus.org/downloads/
需要安装插件HEX-Editor
Visual Studio Code
https://code.visualstudio.com/
安装Hexdump
Atom
https://atom.io/
https://atom.io/packages/hex-view
Sublime Text
https://www.sublimetext.com/
安装HexViewer
超大文件可以只读取前1024字节内容来查看。
https://bbs.csdn.net/topics/350091771
printf "\001\002\003\004" | head -c 3 | tail -c 2 | od -t x1
ping qq.com > bigFile
head -c 32 bigFile > bigFile.32
dd if=bigFile of=bigFile.1k bs=1 skip=0 count=32