pg.
pg.

Reputation: 2531

How to do Multi file find and replace from unix prompt

I want to replace 'localhost' with an actual ip like '1.1.1.1' in every file in a directory including subfolders, plus I want it to log the filenames it changed. I'm having a difficult time doing this, what command should I use?

Upvotes: 1

Views: 925

Answers (2)

vpit3833
vpit3833

Reputation: 7951

find /path/to/all/files -type f -exec sed -i 's/localhost/IP/g' {}\; should work. Or you get an idea of how to make sed work on every file that find finds.

Upvotes: 1

Matthew Flaschen
Matthew Flaschen

Reputation: 284816

grep -r --files-with-matches localhost *|tee changed_files|xargs sed -i 's/localhost/1.1.1.1/g'

The files changed will be logged to changed_files.

Upvotes: 2

Related Questions