Find and Replace string in a directory on Linux

Server Norckon 214℃ 0评论

1464160367-8333-div3

INTRODUCE

When I changed website domain, I had a biggest problem is how to replace all domain on numerous web page file. In this time, I can using some command provided by Linux to solve this problem.

HOW TO

Find file in /usr/local/nginx/html which include string fcsys.us

grep -lr 'fcsys.us' /usr/local/nginx/html/*

Replace all string in single file by vim editor

:%s/old/new/g

NOTE:

  • %: Replace all string in each line.
  • g: Replace all matched strings.

Replace all string for all files in a directory

sed -i "s/old/new/g" `grep old -rl /usr/local/nginx/html`

REFERENCE

AUTHOR

Pekaikon Norckon
2016-05-25 (WED)

喜欢 (0)
COMMENT
Cancel Comment
EMOJI

Hi, We need some information

  • Nickname (*)
  • E-Mail (*)
  • Website