如何确定文件的常规文件扩展名

Sea*_* W. 6 shell filenames mime-types

file命令擅长根据我的需要确定文件类型。有没有办法将这些结果映射到传统的文件扩展名?

Jos*_* R. 5

在您的系统上注册的文件扩展名应该是/etc/mime.types. 因此,假设您设法将文件类型从 的输出中提取file到名为 的变量中type,您可以简单地:

grep -i "$type" /etc/mime.types | awk '{$1="";print $0}'
Run Code Online (Sandbox Code Playgroud)

或者(如评论中的 200_success 所建议的),您可以awk单独使用:

awk -v IGNORECASE=1 '/ENVIRON["type"]/{$1="";print $0}'
Run Code Online (Sandbox Code Playgroud)

例子

$ type=Perl
$ grep -i "$type" /etc/mime.types | awk '{$1="";print $0}'
 pl pm
Run Code Online (Sandbox Code Playgroud)