10

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 }'

3 Answers 3

8

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, now I know why it was so slow ;)
1
#!/bin/bash
find $1 | file -if- | grep $2 | awk -F: '{print $1}'

6 Comments

won't this locate files ending in .css, and then open them and search for "text/css"? if so, then that wasn't what I meant. I don't want to rely on filename extensions.
That's what I can understand from your question, can you be more specific in how do you want to determine the mime type? and for which file location?
for example, if I execute my script like this: " findbytype.sh /home/me audio", it shows all files reported to be audio by the command " file -i filename "
yes, thank you. this is kinda faster, but the problem is that grep looks for $2 in both the output of find and file, so even if file -i output doesn't contain $2 the file's name or any of it's parent directories may contain $2
That's is not a problem, you can use some fileextensions to narrow the iterations but this is scripting. you are confusing it with a real programming language
|
0
#!/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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.