If your sed(1)
has a -i
option, then use it like this:
for i in *; do
sed -i 's/foo/bar/' $i
done
If not, there are several ways variations on the following depending on which language you want to play with:
ruby -i.bak -pe 'sub(%r{foo}, 'bar')' *
perl -pi.bak -e 's/foo/bar/' *
This works using grep without needing to use perl or find.
grep -rli 'old-word' * | xargs -i@ sed -i 's/old-word/new-word/g' @
If your sed(1)
has a -i
option, then use it like this:
for i in *; do
sed -i 's/foo/bar/' $i
done
If not, there are several ways variations on the following depending on which language you want to play with:
ruby -i.bak -pe 'sub(%r{foo}, 'bar')' *
perl -pi.bak -e 's/foo/bar/' *
find . -type f -print0 | xargs -0 <sed/perl/ruby cmd>
will process multiple space contained file names at once loading one interpreter per batch. Much faster.
If your sed(1)
has a -i
option, then use it like this:
for i in *; do
sed -i 's/foo/bar/' $i
done
If not, there are several ways variations on the following depending on which language you want to play with:
ruby -i.bak -pe 'sub(%r{foo}, 'bar')' *
perl -pi.bak -e 's/foo/bar/' *
This appears to be what you want, based on the example you gave:
sed -i 's/foo/bar/g' *
It is not recursive (it will not descend into subdirectories). For a nice solution replacing in selected files throughout a tree I would use find:
find . -name '*.html' -print -exec sed -i.bak 's/foo/bar/g' {} \;
The *.html
is the expression that files must match, the .bak
after the -i
makes a copy of the original file, with a .bak extension (it can be any extension you like) and the g
at the end of the sed expression tells sed to replace multiple copies on one line (rather than only the first one). The -print
to find is a convenience to show which files were being matched. All this depends on the exact versions of these tools on your system.
This appears to be what you want, based on the example you gave:
sed -i 's/foo/bar/g' *
It is not recursive (it will not descend into subdirectories). For a nice solution replacing in selected files throughout a tree I would use find:
find . -name '*.html' -print -exec sed -i.bak 's/foo/bar/g' {} \;
The *.html
is the expression that files must match, the .bak
after the -i
makes a copy of the original file, with a .bak extension (it can be any extension you like) and the g
at the end of the sed expression tells sed to replace multiple copies on one line (rather than only the first one). The -print
to find is a convenience to show which files were being matched. All this depends on the exact versions of these tools on your system.
I like and used the above solution or a system wide search and replace among thousands of files:
find -name '*.htm?' -print -exec sed -i.bak 's/foo/bar/g' {} \;
I assume with the '*.htm?' instead of .html it searches and finds .htm and .html files alike.
I replace the .bak with the more system wide used tilde (~) to make clean up of backup files easier.
This works using grep without needing to use perl or find.
grep -rli 'old-word' * | xargs -i@ sed -i 's/old-word/new-word/g' @
This appears to be what you want, based on the example you gave:
sed -i 's/foo/bar/g' *
It is not recursive (it will not descend into subdirectories). For a nice solution replacing in selected files throughout a tree I would use find:
find . -name '*.html' -print -exec sed -i.bak 's/foo/bar/g' {} \;
The *.html
is the expression that files must match, the .bak
after the -i
makes a copy of the original file, with a .bak extension (it can be any extension you like) and the g
at the end of the sed expression tells sed to replace multiple copies on one line (rather than only the first one). The -print
to find is a convenience to show which files were being matched. All this depends on the exact versions of these tools on your system.
This is actually easier than it seems.
grep -Rl 'foo' ./ | xargs -n 1 -I % sh -c "ls %; sed -i 's/foo/bar/g' %";
Easy peasy. If you get a good grasp on find, grep, xargs, sed, and awk, almost nothing is impossible when it comes to text file manipulation in bash :)
find . -type f -print0 | xargs -0 <sed/perl/ruby cmd>
will process multiple space contained file names at once loading one interpreter per batch. Much faster.
The answer already given of using find and sed
find -name '*.html' -print -exec sed -i.bak 's/foo/bar/g' {} \;
is probably the standard answer. Or you could use perl -pi -e s/foo/bar/g'
instead of the sed
command.
For most quick uses, you may find the command rpl is easier to remember. Here is replacement (foo -> bar), recursively on all files in the current directory:
rpl -R foo bar .
It's not available by default on most Linux distros but is quick to install (apt-get install rpl
or similar).
However, for tougher jobs that involve regular expressions and back substitution, or file renames as well as search-and-replace, the most general and powerful tool I'm aware of is repren, a small Python script I wrote a while back for some thornier renaming and refactoring tasks. The reasons you might prefer it are:
Check the README for examples.
If your sed(1)
has a -i
option, then use it like this:
for i in *; do
sed -i 's/foo/bar/' $i
done
If not, there are several ways variations on the following depending on which language you want to play with:
ruby -i.bak -pe 'sub(%r{foo}, 'bar')' *
perl -pi.bak -e 's/foo/bar/' *
I like and used the above solution or a system wide search and replace among thousands of files:
find -name '*.htm?' -print -exec sed -i.bak 's/foo/bar/g' {} \;
I assume with the '*.htm?' instead of .html it searches and finds .htm and .html files alike.
I replace the .bak with the more system wide used tilde (~) to make clean up of backup files easier.
This is actually easier than it seems.
grep -Rl 'foo' ./ | xargs -n 1 -I % sh -c "ls %; sed -i 's/foo/bar/g' %";
Easy peasy. If you get a good grasp on find, grep, xargs, sed, and awk, almost nothing is impossible when it comes to text file manipulation in bash :)
This appears to be what you want, based on the example you gave:
sed -i 's/foo/bar/g' *
It is not recursive (it will not descend into subdirectories). For a nice solution replacing in selected files throughout a tree I would use find:
find . -name '*.html' -print -exec sed -i.bak 's/foo/bar/g' {} \;
The *.html
is the expression that files must match, the .bak
after the -i
makes a copy of the original file, with a .bak extension (it can be any extension you like) and the g
at the end of the sed expression tells sed to replace multiple copies on one line (rather than only the first one). The -print
to find is a convenience to show which files were being matched. All this depends on the exact versions of these tools on your system.
The answer already given of using find and sed
find -name '*.html' -print -exec sed -i.bak 's/foo/bar/g' {} \;
is probably the standard answer. Or you could use perl -pi -e s/foo/bar/g'
instead of the sed
command.
For most quick uses, you may find the command rpl is easier to remember. Here is replacement (foo -> bar), recursively on all files in the current directory:
rpl -R foo bar .
It's not available by default on most Linux distros but is quick to install (apt-get install rpl
or similar).
However, for tougher jobs that involve regular expressions and back substitution, or file renames as well as search-and-replace, the most general and powerful tool I'm aware of is repren, a small Python script I wrote a while back for some thornier renaming and refactoring tasks. The reasons you might prefer it are:
Check the README for examples.
Source: Stackoverflow.com