Hi All,

I am trying to execute a find command to retrieve all files of a specific type, e.g. images. Rather than using an OR'd list of suffixes with ~every~ image type, I wrote a small script that returns the mime-type (image, application, executable, etc). The problem is getting it into the find command. The command: find ./ -type f -exec test $( ftype {} ) = 'image' \; throws an error. The message from my "ftype" script is that it can't find the file named "{}". I know the "ftype" script works if used alone.

Here's the "ftype" script:

#!/usr/bin/bash
# SYNTAX: ftype [option] filename

# ... do argument handling ...

if [ ! -f "$filename" ]; then 
	echo -e "\nERR: File '$filename' not found\n"  1>&2
	exit 1
fi

t=$( file --mime-type "$filename" )
t1=${t#*: }
echo "${t1%/*}"

I've never tried using the find command this way, so I don't know if the problem is something in the "ftype" script, or if I'm doing the find command incorrectly. And I'm trying to avoid using a for loop followed by an if statement. Can anyone help me out here?

Thanks,
Mitch

P.S. This is not homework - at my job I frequently have to search thru a lot of files for keywords, and searching only applicable files (or filtering out non-applicable files) would help make the search faster.

try this: find ./ -type f -exec test $( `ftype {}` ) = 'image' \;

Hi Kruptein,

I tried your suggestion (as well as several variants moving the quotes around and adding parenthesis), but it still throws an error:

ERR: File '{}' not found

test: missing argument after `)'

The second line is repeated for each file the 'find' command finds, but the first error only ever appears once.

The following will find all files of some type of 'image'.

The student's first exercise is to convert, in the script, the argument 1 word into a grep-style "a|b|c|d|..." argument in order to select only picture images (i.e., excluding disk images, et al).

The student's next exercise is to make a complete set of different 'argument 1' words so she can search for different kinds of things by specifying a single keyword.

Script ftype:

#! /bin/bash
file "$2" | grep "$1" > /dev/null 2>&1

Usage:

find . -type f -exec ftype image {} \; -print

Nice simple solution, Fest3r - just one slight snag, sorry if I'm being a little bit too picky, but this solution will also pick up files whose name contains the text "image". I think it just needs "file -b $2" in the first script.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.