Bash:按 MIME 类型查找文件的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6802914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash: Script for finding files by mime-type
提问by masamunedark
First, I am not experienced in scripting, so be gentle with me
首先,我在脚本方面没有经验,所以对我温柔一点
Anyway, I tried making a script for finding files by mime-type ( audio, video, text...etc), and here's the poor result I came up with.
无论如何,我尝试制作一个脚本来按 mime 类型(音频、视频、文本等)查找文件,这是我想出的糟糕结果。
#!/bin/bash
FINDPATH=""
FILETYPE=""
locate $FINDPATH* | while read FILEPROCESS
do
if file -bi "$FILEPROCESS" | grep -q "$FILETYPE"
then
echo $FILEPROCESS
fi
done
It works, but as you could guess, the performance is not so good.
它可以工作,但正如您所猜测的那样,性能并不是那么好。
So, can you guys help me make it better ? and also, I don't want to rely on files extensions.
所以,你们能帮我把它做得更好吗?而且,我不想依赖文件扩展名。
Update:
更新:
Here's what I am using now
这是我现在使用的
#!/bin/bash
FINDPATH=""
find "$FINDPATH" -type f | file -i -F "::" -f - | awk -v FILETYPE="" -F"::" ' ~ FILETYPE { print }'
采纳答案by Eric Fortis
#!/bin/bash
find | file -if- | grep | awk -F: '{print }'
回答by jm666
Forking (exec) is expensive. This runs the filecommand only once, so it is fast:
分叉 ( exec) 是昂贵的。这个file命令只运行一次,所以速度很快:
find . -print | file -if - | grep "what you want" | awk -F: '{print }'
or
或者
locate what.want | file -if -
check man file
查看 man file
-i #print mime types
-f - #read filenames from the stdin

