I stumbled upon following issue: on a single line, I would like to alter the result of my find
command to do a specific cp
depending of the files I found, with a destination name that depends of the files returned by the find
.
Here are my files,
$ find jcho -name *.data
jcho/category1/001.data
jcho/category2/002.data
and they must be copied to
jcho2/category1_001.data
jcho2/category2_002.data
I tried this,
$ find . -name *.data \
-exec cp {} ` echo {} | sed -re 's/(jcho\/)(category[0-9]*)(\/)(.*data)/jcho2\/\2_\4/' ` \;
but it says it want to copy to same file -- my substitution is not made; got below error:
cp: './jcho/category1/001.data' and './jcho/category1/001.data' are the same file
So I tried something with a subshell to which I give the result of the find. This worked (a little).
find jcho -name *.data -exec sh -c \
'f="${0}"; d=$(echo ${f} | sed -re 's/0/2/' ); cp ${f} ${d} ' {} \;
==>
find jcho -name 2*.data
jcho/category1/201.data
jcho/category2/202.data
If I could include /
in the sed
pattern, my solution would be at hand...
But I get:
find jcho -name *.data -exec sh -c \
'f="${0}"; d=$(echo ${f} | sed -re 's/(jcho\/)(category[0-9]*)(\/)(.*data)/jcho2\/\2_\4/' ); cp ${f} ${d} ' {} \;
-ksh: syntax error: `(' unexpected
... I tried escaping the /
with \
, with \\
... not better.
idem with
find jcho -name *.data -exec sh -c \
'f="${0}"; d=$(echo ${f} | sed -re 's~\([^)]*\)/\([^()]*\)$~\1_\2~' ); cp ${f} ${d} ' {} \;
Thank you for your help!