I am trying to execute multiple sed operations on the find -exec operation. My code looks like this:
find . -name '*.html.haml' -exec sed -i '' 's/restaurant_id/company_id/g' && sed -i '' 's/restaurants/companies/g' && sed -i '' 's/restaurant/company/g' && sed -i '' 's/Restaurants/Companies/g' && sed -i '' 's/Restaurant/Company/g' "{}" \;
This seems not to work. How could I do that?
Error:
find: -exec: no terminating ";" or "+"
your shell presumably splits this command to
find . -name '*.html.haml' -exec sed -i '' 's/restaurant_id/company_id/g'
and&&
andsed -i ...
try quoting the exec command
find . -name '*.html.haml' -exec "sed -i '' 's/restaurant_id/company_id/g' && sed -i ..."
another approach is an extern sed-script you can call with a single command from exec
As others have said, the problem is that the
&&
is interpereted to execute the sed command after thefind
command is finished, instead of passing one string of commands to be executed on each file.The easiest way to achieve your desired result is to combine this all into one
sed
command with semicolons. Like so:The
&&
s will be interpreted by the shell. Quote them all to prevent this'&&'
You'll need to add
"{}"
to each sed command.Alternatively you could use the
-e
option of sed to only execute sed once with multiple scripts.