Recursive search and replace in text files on Mac and Linux
OS X uses a mix of BSD and GNU tools, so best always check the documentation (although I had it that less
didn't even conform to the OS X manpage):
sed takes the argument after -i
as the extension for backups. Provide an empty string (-i ''
) for no backups.
The following should do:
LC_ALL=C find . -type f -name '*.txt' -exec sed -i '' s/this/that/ {} +
The -type f
is just good practice; sed will complain if you give it a directory or so.-exec
is preferred over xargs
; you needn't bother with -print0
or anything.The {} +
at the end means that find
will append all results as arguments to one instance of the called command, instead of re-running it for each result. (One exception is when the maximal number of command-line arguments allowed by the OS is breached; in that case find
will run more than one instance.)
As an alternative solution, I'm using this one on Mac OSX 10.7.5
grep -ilr 'old-word' * | xargs -I@ sed -i '' 's/old-word/new-word/g' @
Credit goes to: Todd Cesere's answer