Linux常用命令-file

命令

file

描述

determine file type
显示文件类型

用法

1
file [OPTION...] [FILE...]

选项

1
2
3
4
5
6
7
8
9
10
Options:
-b 不显示文件名,只显示文件类型
-f 从文件中读取要检查的文件名
-F 指定文件名和文件类型之间的分隔符,默认为:号
-i 显示MIME类型和编码信息
-L 跟随链接跳转,即检查源文件而非链接文件
-h 不跟随链接跳转,即检查链接文件本身,默认
-N 输出内容不填充空格对齐,即文件名和文件类型之间只有一个空格
-s 检查特殊文件,如磁盘文件/dev/sda1
-0 不拆分特殊文件名,如文件名包含空格时不会拆分为多个文件名

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
$ file a aaa.html b.sh
a: ASCII text
aaa.html: HTML document, ASCII text
b.sh: Bourne-Again shell script, ASCII text executable
# -b不显示文件名
$ file -b a aaa.html b.sh
ASCII text
HTML document, ASCII text
Bourne-Again shell script, ASCII text executable
$ more filelist
a
aaa
aaa.html
a.tar
b.sh
# -f指定文件名列表
$ file -f filelist
a: ASCII text
aaa: ASCII text
aaa.html: HTML document, ASCII text
a.tar: POSIX tar archive (GNU)
b.sh: Bourne-Again shell script, ASCII text executable
# -F指定分隔符
$ file -F"=" a aaa.html b.sh
a= ASCII text
aaa.html= HTML document, ASCII text
b.sh= Bourne-Again shell script, ASCII text executable
# -i显示MIME信息
$ file -i a
a: text/plain; charset=us-ascii
$ file -i aaa.html
aaa.html: text/html; charset=us-ascii
$ ll a b
-rw-r--r-- 1 root root 140 Feb 7 23:50 a
lrwxrwxrwx 1 root root 1 Feb 8 13:04 b -> a
# 默认检查链接文件本身
$ file a b
a: ASCII text
b: symbolic link to 'a'
# -L检查链接的源文件
$ file -L a b
a: ASCII text
b: ASCII text
# -h检查链接文件本身
$ file -h a b
a: ASCII text
b: symbolic link to 'a'
# 默认填充空格对齐
$ file a aaa.html
a: ASCII text
aaa.html: HTML document, ASCII text
# -N不填充空格对齐
$ file -N a aaa.html
a: ASCII text
aaa.html: HTML document, ASCII text
$ file /dev/sda1
/dev/sda1: block special
# -s指定检查特殊文件
$ file -s /dev/sda1
/dev/sda1: SGI XFS filesystem data (blksz 4096, inosz 512, v2 dirs)
$ file -s a
a: ASCII text
$ file -s /dev/sda2
/dev/sda2: LVM2 PV (Linux Logical Volume Manager), UUID: pMp2wr-fcQt-RD12-ePVv-BDUn-SLFx-SLfW33, size: 20400046080