用\找到-exec;运行但+终结器失败,并且#34;缺少-exec"

时间:2016-11-17 18:07:10

标签: bash unix find

有时find命令拒绝使用+终结符执行-exec,但只需将+更改为\;即可运行exec。

在以下设置之后考虑:

find_args=( -type f -name "*users*" -cmin -60 )
rsync_args=( -avhP -e 'ssh  -i /home/some_user/.ssh/id_rsa -c arcfour' )
dest=some_user@some_host:/some/destination/

这有效:

# runs rsync once per file, thus slower than it should need to be
find . "${find_args[@]}" -exec rsync "${rsync_args[@]}" {} "$dest" \;

...但是这个失败了:

# exactly the same, except for + rather than \;
# ...should use the same rsync call for multiple files.
find . "${find_args[@]}" -exec rsync "${rsync_args[@]}" {} "$dest" +

...错误为find: missing argument to '-exec'

我正在使用GNU findutils 4.4.2,它被记录为支持+参数。

1 个答案:

答案 0 :(得分:2)

find -exec ... {} +中,+必须在{}之后 (因此插入的参数必须位于尾随位置)。您的命令不符合此要求。

请考虑以下解决方法:

find . -type f -name "*users*" -cmin -60 \
  -exec sh -c 'rsync -avhP -e "ssh -i /home/some_user/.ssh/id_rsa -c arcfour" "$@" some_user@some_host:/some/destination/' _ {} +

因为sh -c [...]行比它运行的rsync行长,所以传递给前者的任何参数列表都保证适用于后者,因此扩展"$@"将始终成功