Skip to content

Instantly share code, notes, and snippets.

@rossta
Created February 4, 2014 22:42
Show Gist options
  • Select an option

  • Save rossta/8813895 to your computer and use it in GitHub Desktop.

Select an option

Save rossta/8813895 to your computer and use it in GitHub Desktop.
Find and replace in directory on the shell
# example: replacing :mocha with :rspec as mock framework
find . -name '*_spec.rb' -exec sed -i.old -e 's/returns/and_return/' {} \;
find . -name '*_spec.rb' -exec sed -i.old -e 's/expects/should_receive/' {} \;
find . -name '*_spec.rb' -exec sed -i.old -e 's/stubs/stub/' {} \;
find . -name '*.old' -exec rm {} \;
@jcmuller
Copy link

jcmuller commented Feb 5, 2014

Totally forgot you can do this:

find . -name '*_spec.rb' -exec sed -i.old \ 
  -e 's/returns/and_return/' \
  -e 's/expects/should_receive/' \
  -e 's/stubs/stub/' {} \;
find . -name '*.old' -exec rm {} \;

sed can take multiple commands — expressions — when you prepend them with the -e switch. I’ve gotten myself in the habit of specifying that for sed and grep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment