Take the 2-minute tour ×

I need to find all of the TIFFs in a directory, recursively, but ignore some artifacts (basically all hidden files) that also happen to end with ".tif". This command:

find . -type f -name '*.tif' ! -name '.*'

works exactly how I want it on the command line, but inside a bash script it doesn't find anything. I've tried replacing ! with -and -not and--I think--just about every escaping permutation I can think of and/or recommended by the googleshpere, e.g. .\*, leaving out single quotes, etc. Obviously I'm missing something, any help is appreciated.

EDIT: here's the significant part of the script; the directory it's doing the find on is parameterized, but I've been debugging with it hard-coded; it makes no difference:


#get the absolute path to $DIR
DIR=$(cd $DIR; pwd)

FIND_CMD="find $DIR -type f -name '*.tif' ! -name '.*'"
if [ $RECURSIVE == 1 ]; then
    FIND_CMD="$FIND_CMD -maxdepth 1"

for in_img in $($FIND_CMD | sort); do
    echo $in_img # for debugging
share|improve this question
please include an excerpt from your script –  pb2q Jun 18 '12 at 19:52
Are you calling the script from the directory that contains the .tiff files? –  Tim Pote Jun 18 '12 at 19:53
What is the working directory of your script? That's where the find command is looking for files matching '*.tif'. –  chepner Jun 18 '12 at 19:53
Both versions seem to work for me. –  notfed Jun 18 '12 at 19:54
What happens if you remove everything after .? Does it still not find anything? –  Laurence Gonsalves Jun 18 '12 at 19:55
show 2 more comments

2 Answers

up vote 1 down vote accepted

It was related to having the expression stored in a variable. The solution was to use eval, which of course would be the right thing to do anyway. Everything is the same as above except at the start of the for loop:

for in_img in $(eval $FIND_CMD | sort); do
share|improve this answer
I'll strongly recommend against using eval this way, as it can cause some truly bizarre bugs. You're better off storing the command in an array as described in that link (or not storing it at all). Also, see BashFAQ #050. –  Gordon Davisson Jun 23 '12 at 3:17
add comment

To not find hidden files you can use the following:


It checks the filename and doesn't show (! in the parenthesis) the files beginning with a dot, which are the hidden files.

share|improve this answer
Same problem, though thanks for the alternative. I think it has to do with the fact that I'm building the expression and storing it in a variable. –  JStroop Jun 18 '12 at 22:22
add comment

Your Answer


