Last Updated: February 25, 2016
·
473
· andrewhao

Searching and replacing a directory of files with `find`, `sed` and friends.

Here's how we globally search for and replace a deprecated relative require statement in *_spec.rb files.

find . -type f -name "*spec.rb" -exec sed -i '' 's/^require.*\/spec_helper.*$/require "spec_helper"/g' {} \;

and voila:

diff --git a/spec/controllers/api/book_prices_controller_spec.rb b/spec/controllers/api/book_prices_controller_spec.rb
index 013df6b..27a3c8a 100644
--- a/spec/controllers/api/book_prices_controller_spec.rb
+++ b/spec/controllers/api/book_prices_controller_spec.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/../../spec_helper'
+require "spec_helper"