In Mac OS, how do I find and replace a certain text in all of a directory and its sub directories?

link|improve this question

file/directory names are contents? – mindless.panda May 28 '10 at 18:07
feedback

1 Answer

up vote 5 down vote accepted

Use a combination of find(1) and sed(1):

# Find all files under the directory hierarchy rooted at 'root', and replace
# all instances of the regular expression 'pattern' with 'replacement' in all
# of those files:
find root -type f -exec sed -i~ 's/pattern/replacement/g' '{}' '+'

If you run into command line length limitations, replace the '+' at the end with ';'. This will make it run slower (since it was to fork a new process for each file), but it will not have a danger of too long of a command line for sed.

You can also only do the replacement for certain files by adding appropriate filters to find (e.g. -name *.txt to only replace .txt files).

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.