0

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.

Edited by mstone42: adding PS

4
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Simon Tite
0

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.

1

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

Edited by Fest3er: n/a

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.