Linux之file

1.file

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

2.file 语法

file [-bcLvz][-f <名称文件>][-m <魔法数字文件>...][文件或目录...]

3.file 参数

-b  列出辨识结果时,不显示文件名称。
-c  详细显示指令执行过程,便于排错或分析程序执行的情形。
-f<名称文件>  指定名称文件,其内容有一个或多个文件名称时,让file依序辨识这些文件,格式为每列一个文件名称。
-L  直接显示符号连接所指向的文件的类别。
-m<魔法数字文件>  指定魔法数字文件。
-v  显示版本信息。
-z  尝试去解读压缩文件的内容。
-i 输出mime的字符串 [文件或目录...] 要确定类型的文件列表,多个文件之间使用空格分开,可以使用shell通配符匹配多个文件。

4. file 实例

4.1 b  示例

[root@VM-4-13-centos shell]# ll
总用量 20
drwxr-xr-x 2 root root 4096 9月  27 11:01 awk
-rwxr-xr-x 1 root root   84 9月  28 10:36 file.sh
-rwxr-xr-x 1 root root   99 9月  27 11:24 readarray.sh
drwxr-xr-x 2 root root 4096 9月  28 08:36 sed
-rw-r--r-- 1 root root    8 9月  28 10:05 test.txt
[root@VM-4-13-centos shell]# file -b file.sh 
Bourne-Again shell script, ASCII text executable

4.2 i 示例

[root@VM-4-13-centos shell]# file -i file.sh 
file.sh: text/x-shellscript; charset=us-ascii

5.文件属性判断

 5.1 -e file 判断文件或者目录是否存在,存在返回true,否则返回false。

[root@VM-4-13-centos shell]# ls
awk  file.sh  readarray.sh  sed  test.txt
[root@VM-4-13-centos shell]#  [ -e /home/shell/file.sh ] && echo true || echo false 
true

5.2 -f file 判断文件存在且是普通文件

[root@VM-4-13-centos shell]#  [ -f /home/shell/file.sh ] && echo true || echo false 
true
[root@VM-4-13-centos shell]#  [ -f /home/shell/file.sh1 ] && echo true || echo false 
false
[root@VM-4-13-centos shell]# 

5.3 -d file 判断目录是否存在。

[root@VM-4-13-centos shell]#  [ -d /home/shell/file.sh1 ] && echo true || echo false 
false
[root@VM-4-13-centos shell]#  [ -d /home/shell ] && echo true || echo false 
true

 

posted @ 2022-09-28 11:34  家乐福的搬砖日常  阅读(136)  评论(0编辑  收藏  举报