简体   繁体   中英

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM