0

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:

#!/bin/bash RECURSIVE=1 DIR=$1 #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" fi for in_img in $($FIND_CMD | sort); do echo $in_img # for debugging #stuff done 
7
  • 1
    please include an excerpt from your script Commented Jun 18, 2012 at 19:52
  • Are you calling the script from the directory that contains the .tiff files? Commented Jun 18, 2012 at 19:53
  • 1
    What is the working directory of your script? That's where the find command is looking for files matching '*.tif'. Commented Jun 18, 2012 at 19:53
  • Both versions seem to work for me. Commented Jun 18, 2012 at 19:54
  • What happens if you remove everything after .? Does it still not find anything? Commented Jun 18, 2012 at 19:55

2 Answers 2

1

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

1 Comment

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.
0

To not find hidden files you can use the following:

 find . \( ! -regex '.*/\..*' \) -type f -name "*.tif" 

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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.