cowboybebop
cowboybebop

Reputation: 2345

Unix find with exec + and extra variables

I have a need for recursively looking through a directory and pass all files with a certain extension to a script. In addition it also takes some optional command lines arguments from the user. To put it all together, I did this

usage="blah"
while [ "$1" != "" ]; do
  case $1 in
    -arg1 )  shift
             arg1=$1
             ;;
    -arg2 )  shift
             arg2=$1
             ;;
    * )      echo $usage
             exit 1
  esac
  shift
done

find . -name "*.ext" -exec bash -c 'command "$0" arg1="${arg1:-default}" arg2=${arg2:-default}' "{}" \+

However this runs the 'command' command and only passes one file in. How do I get to send the list of files as an argument as well as keep the command line arguments?

EDIT: The command should run only once for the list of matched files.

Upvotes: 0

Views: 456

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295403

You don't need bash -c here at all!

find . -name '*.ext' \
  -exec yourcommand arg1="${arg1:-default}" arg2="${arg2:-default}" {} +

See edit history for a discussion of exactly how the old version was broken and how to do more targeted fixes for cases when you really do need a shell.

Upvotes: 1

Related Questions