Best way to do a find/replace in several files?

Viewed 24461

what's the best way to do this? I'm no command line warrior, but I was thinking there's possibly a way of using grep and cat.

I just want to replace a string that occurs in a folder and sub-folders. what's the best way to do this? I'm running ubuntu if that matters.

6 Answers
find . -type f -print0 | xargs -0 -n 1 sed -i -e 's/from/to/g'

The first part of that is a find command to find the files you want to change. You may need to modify that appropriately. The xargs command takes every file the find found and applies the sed command to it. The sed command takes every instance of from and replaces it with to. That's a standard regular expression, so modify it as you need.

If you are using svn beware. Your .svn-directories will be search and replaced as well. You have to exclude those, e.g., like this:

find . ! -regex ".*[/]\.svn[/]?.*" -type f -print0 | xargs -0 -n 1 sed -i -e 's/from/to/g'

or

find . -name .svn -prune -o -type f -print0 | xargs -0 -n 1 sed -i -e 's/from/to/g'

As Paul said, you want to first find the files you want to edit and then edit them. An alternative to using find is to use GNU grep (the default on Ubuntu), e.g.:

grep -r -l from . | xargs -0 -n 1 sed -i -e 's/from/to/g'

You can also use ack-grep (sudo apt-get install ack-grep or visit http://petdance.com/ack/) as well, if you know you only want a certain type of file, and want to ignore things in version control directories. e.g., if you only want text files,

ack -l --print0 --text from | xargs -0 -n 1 sed -i -e 's/from/to/g'
# `from` here is an arbitrary commonly occurring keyword

An alternative to using sed is to use perl which can process multiple files per command, e.g.,

grep -r -l from . | xargs perl -pi.bak -e 's/from/to/g'

Here, perl is told to edit in place, making a .bak file first.

You can combine any of the left-hand sides of the pipe with the right-hand sides, depending on your preference.

The typical (find|grep|ack|ag|rg)-xargs-sed combination has a few problems:

  • Difficult to remember and get correct. Eg, forgetting the xargs -r option will run the command even when no files are found, potentially causing problems.
  • Retrieving the file list, and the actual replacement uses different CLI tools and can have a different search behaviour.

These problems were big enough for such an invasive and dangerous operation as recursive search-and-replace, to start the development of a dedicated tool: mo.

Early tests seem to indicate that its performance is between ag and rg and it solves following problems I encounter with them:

  • A single invocation can filter on filename and content. Following command searches for the word bug in all source files that have a v1 indication:

    mo -f 'src/.*v1.*' -p bug -w

  • Once the search results are OK, actual replacement for bug with fix can be added:

    mo -f 'src/.*v1.*' -p bug -w -r fix

comment() { 
}
doc() { 
}
function agr { 
doc 'usage: from=sth to=another agr [ag-args]'
comment -l --files-with-matches

ag -0 -l "$from" "${@}" | pre-files "$from" "$to"
}
pre-files() {
doc 'stdin should be null-separated list of files that need replacement; $1 the string to replace, $2 the replacement.'
comment '-i backs up original input files with the supplied extension (leave empty for no backup; needed for in-place replacement.)(do not put whitespace between -i and its arg.)'
comment '-r, --no-run-if-empty
              If  the  standard input does not contain any nonblanks,
              do not run the command.  Normally, the command  is  run
              once  even  if there is no input.  This option is a GNU
              extension.'

AGR_FROM="$1" AGR_TO="$2" xargs -r0 perl -pi.pbak -e 's/$ENV{AGR_FROM}/$ENV{AGR_TO}/g'
}

You can use it like this:

from=str1 to=sth agr path1 path2 ...

Supply no paths to make it use the current directory. Note that ag, xargs, and perl need to be installed and on PATH.

Related