【问题标题】:Extract only part of a text just before delimiter '/'?仅提取分隔符“/”之前的部分文本?
【发布时间】:2017-06-27 07:04:49
【问题描述】:

我需要找到文件夹中所有文件的所有mime-type。我像这样写了一个小sn-p。

 for i in $(find /home/someFolder -type f);do file -b $i --mime-type;done

这将返回以下输出。

text/html
application/msword
application/msword
text/html
text/html
text/html
application/msword
application/vnd.ms-excel

但是我能否从这些输出中获取提取的信息,例如 htmlmswordpng

html
msword
msword

是否有任何库可以用来了解文件的详细信息,其中应包括文件中的 mime 类型、大小、字符串以及任何其他行为信息。任何建议都会有所帮助。

【问题讨论】:

    标签: bash shell file mime


    【解决方案1】:

    这样做:

    find . -type f -print0 | xargs -0L1 file -b --mime-type | cut -d/ -f2
    

    比您的 for 方法更快、更安全。 (显然,将. 替换为您要开始搜索的路径。)

    【讨论】:

    • 由于在该过程中不会保留文件名,也许添加带有sort -u 的最后一个管道阶段以避免重复是否有意义?
    • 是的,sort -u 是有道理的。您甚至可以sort | uniq -c 获取各种类型的计数...
    猜你喜欢
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多