Is there a way to get find
to execute a function I define in the shell? For example:
dosomething () {
echo "doing something with $1"
}
find . -exec dosomething {} \;
The result of that is:
find: dosomething: No such file or directory
Is there a way to get find
's -exec
to see dosomething
?
Have the script call itself, passing each item found as an argument:
When you run the script by itself, it finds what you are looking for and calls itself passing each find result as the argument. When the script is run with an argument, it executes the commands on the argument and then exits.
It is not possible to executable a function that way.
To overcome this you can place your function in a shell script and call that from
find
Now use it in find as:
I would avoid using
-exec
altogether. Why not usexargs
?Put the function in a separate file and get
find
to execute that.Shell functions are internal to the shell they're defined in;
find
will never be able to see them.Since only the shell knows how to run shell functions, you have to run a shell to run a function. You also need to mark your function for export with
export -f
, otherwise the subshell won't inherit them: