mas*_*ark 9 bash file find mime-types
首先,我没有脚本编写经验,所以要对我很温柔
无论如何,我尝试制作一个脚本来通过mime-type(音频,视频,文本等)查找文件,这就是我想出的糟糕结果.
#!/bin/bash
FINDPATH="$1"
FILETYPE="$2"
locate $FINDPATH* | while read FILEPROCESS
do
if file -bi "$FILEPROCESS" | grep -q "$FILETYPE"
then
echo $FILEPROCESS
fi
done
Run Code Online (Sandbox Code Playgroud)
它可以工作,但正如你猜测的那样,性能并不是那么好.
所以,你们能帮助我做得更好吗?而且,我不想依赖文件扩展名.
更新:
这就是我现在使用的
#!/bin/bash
FINDPATH="$1"
find "$FINDPATH" -type f | file -i -F "::" -f - | awk -v FILETYPE="$2" -F"::" '$2 ~ FILETYPE { print $1 }'
Run Code Online (Sandbox Code Playgroud)
分叉(exec)很贵.这只运行exec命令一次 - 所以,它很快:
find . -print | file -if - | grep "what you want" | awk -F: '{print $1}'
Run Code Online (Sandbox Code Playgroud)
要么
locate what.want | file -if -
Run Code Online (Sandbox Code Playgroud)
校验 file
-i #print mime types
-f - #read filenames from the stdin
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
find $1 | file -if- | grep $2 | awk -F: '{print $1}'
Run Code Online (Sandbox Code Playgroud)