bash - Recursively rename files using find and sed -
i want go through bunch of directories , rename files end in _test.rb end in _spec.rb instead. it's i've never quite figured out how bash time thought i'd put effort in nailed. i've far come short though, best effort is:
find spec -name "*_test.rb" -exec echo mv {} `echo {} | sed s/test/spec/` \;
nb: there's echo after exec command printed instead of run while i'm testing it.
when run output each matched filename is:
mv original original
i.e. substitution sed has been lost. what's trick?
this happens because sed
receives string {}
input, can verified with:
find . -exec echo `echo "{}" | sed 's/./foo/g'` \;
which prints foofoo
each file in directory, recursively. reason behavior pipeline executed once, shell, when expands entire command.
there no way of quoting sed
pipeline in such way find
execute every file, since find
doesn't execute commands via shell , has no notion of pipelines or backquotes. gnu findutils manual explains how perform similar task putting pipeline in separate shell script:
#!/bin/sh echo "$1" | sed 's/_test.rb$/_spec.rb/'
(there may perverse way of using sh -c
, ton of quotes in 1 command, i'm not going try.)
Comments
Post a Comment