rreddy
rreddy

Reputation: 55

Grep/Sed: How to do a recursive find/replace of a string?

How to find and replace every occurrence of

httpaccess

with

httpaccessabc

in every file of name "access.html" in a particular folder

Upvotes: 1

Views: 840

Answers (3)

Lalit Somnathe
Lalit Somnathe

Reputation: 111

If you know the folder for access.html then :-

sed -i.bak 's/httpaccess/httpaccesabc/g' access.html

(or)

sed -i.bak 's/httpaccess/&abc/g' access.html

Upvotes: 1

sjsam
sjsam

Reputation: 21965

find is your friend

 find . -type f -name 'access.html' \
 -exec sed -i.bak 's/httpaccess/&abc/g' {} \;

Edit

To replace whole pattern use :

 find . -type f -name 'access.html' \
 -exec sed -i.bak 's/abcde/wazsde/g' {} \;

Notes

  1. Replace . with /your/path of concern.
  2. The \ at the end of first line is just to split the command into two lines for more readability.
  3. The g option with sed s command is for global substitution.

Upvotes: 2

Tom Fenech
Tom Fenech

Reputation: 74645

shopt -s globstar
sed -i.bak 's/httpaccess/&abc/g' **/access.html
  • Use globstar with ** to match your filename, recursively.
  • Use sed -i to perform an in-place substitution.

This will create backup files with a suffix .bak. To unset the shell option, use shopt -u globstar afterwards.

Upvotes: 2

Related Questions