bash - How to use >> inside find -exec statement? -
from time time have append text @ end of bunch of files. find these files find
.
i've tried
find . -type f -name "test" -exec tail -n 2 /source.txt >> {} \;
this results in writing last 2 lines /source.txt
file named {}
many times file found matching search criteria.
i guess have escape >>
somehow far wasn't successful.
any appreciated.
-exec
takes 1 command (with optional arguments) , can't use bash operators in it.
so need wrap in bash -c '...'
block, executes between '...'
in new bash shell.
find . -type f -name "test" -exec bash -c 'tail -n 2 /source.txt >> "$1"' bash {} \;
note: after '...'
passed regular arguments, except start @ $0 instead of $1. bash
after '
used placeholder match how expect arguments , error processing work in regular shell, i.e. $1 first argument , errors start bash
or meaningful
if execution time issue, consider doing export variable="$(tail -n 2 /source.txt)"
, using "$variable"
in -exec
. write same thing, unlike using tail
in -exec
, change if file changes. alternatively, can use -exec ... +
, pair tee
write many files @ once.
Comments
Post a Comment