find -exec a shell function in Linux?

2020-01-27 09:52发布

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?

13条回答
\"骚年 ilove
2楼-- · 2020-01-27 10:18

I find the easiest way as follow, repeating two commands in single do

func_one () {
  echo "first thing with $1"
}

func_two () {
  echo "second thing with $1"
}

find . -type f | while read file; do func_one $file; func_two $file; done
查看更多
登录 后发表回答