Bash:按 mime 类型查找文件的脚本

发布于 2024-11-26 01:57:03 字数 590 浏览 0 评论 0原文

首先,我在脚本编写方面没有经验,所以请对我温柔点。

无论如何,我尝试制作一个通过 mime 类型(音频、视频、文本...等)查找文件的脚本,这是我得出的糟糕结果。

#!/bin/bash

FINDPATH="$1"
FILETYPE="$2"


locate $FINDPATH* | while read FILEPROCESS

do

   if  file -bi "$FILEPROCESS" | grep -q "$FILETYPE"
   then
      echo $FILEPROCESS
   fi

done

它可以工作,但正如你所猜到的,性能并不是那么好。

那么,你们能帮我做得更好吗?而且,我不想依赖文件扩展名。

更新:

这是我现在使用的

#!/bin/bash

FINDPATH="$1"


find "$FINDPATH" -type f | file -i -F "::" -f - | awk -v FILETYPE="$2"  -F"::" '$2 ~ FILETYPE { print $1 }'

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.

#!/bin/bash

FINDPATH="$1"
FILETYPE="$2"


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="$1"


find "$FINDPATH" -type f | file -i -F "::" -f - | awk -v FILETYPE="$2"  -F"::" '$2 ~ FILETYPE { print $1 }'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我们的影子 2024-12-03 01:57:03

分叉 (exec) 的成本很高。这仅运行 file 命令一次,因此速度很快:

find . -print | file -if - | grep "what you want" | awk -F: '{print $1}'

或者

locate what.want | file -if -

检查 man file

-i    #print mime types
-f -  #read filenames from the stdin

Forking (exec) is expensive. This runs the file command only once, so it is fast:

find . -print | file -if - | grep "what you want" | awk -F: '{print $1}'

or

locate what.want | file -if -

check man file

-i    #print mime types
-f -  #read filenames from the stdin
如果没有 2024-12-03 01:57:03
#!/bin/bash
find $1 | file -if- | grep $2 | awk -F: '{print $1}'
#!/bin/bash
find $1 | file -if- | grep $2 | awk -F: '{print $1}'
方觉久 2024-12-03 01:57:03
#!/usr/bin/env bash

mimetypes=$(sed -E 's/\/.*//g; /^$/d; /^#/d' /etc/mime.types | uniq)
display_help(){
    echo "Usage: ${0##*/} [mimetype]"
    echo "Available mimetypes:"
    echo "$mimetypes"
    exit 2
}

[[ $# -lt 1 ]] && display_help

ext=$(sed -E "/^${1}/!d; s/^[^ \t]+[ \t]*//g; /^$/d; s/ /\n/g" /etc/mime.types | sed -Ez 's/\n$//; s/\n/\\|/g; s/(.*)/\.*\\.\\(\1\\)\n/')
find "$PWD" -type f -regex "$ext"
#!/usr/bin/env bash

mimetypes=$(sed -E 's/\/.*//g; /^$/d; /^#/d' /etc/mime.types | uniq)
display_help(){
    echo "Usage: ${0##*/} [mimetype]"
    echo "Available mimetypes:"
    echo "$mimetypes"
    exit 2
}

[[ $# -lt 1 ]] && display_help

ext=$(sed -E "/^${1}/!d; s/^[^ \t]+[ \t]*//g; /^$/d; s/ /\n/g" /etc/mime.types | sed -Ez 's/\n$//; s/\n/\\|/g; s/(.*)/\.*\\.\\(\1\\)\n/')
find "$PWD" -type f -regex "$ext"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文