TIMEX
TIMEX

Reputation: 272382

In Linux, how do I go through each file and do a replace?

Suppose I want to go through each file (recursively too), and replace everything with:

{{ MEDIA_URL }}

with:

{% media_url %}/

What command can I run in Linux to replace the former with the latter, in all my files, recursively?

Upvotes: 1

Views: 194

Answers (2)

ciceron
ciceron

Reputation: 586

As much as possible, I use xargs instead of -exec. With a -exec, you'll have one process launched for each file while with xargs you will have only one process. You must use the sed option -s for that. This command is faster when you have several files:

find ./ -type f | xargs sed -s -i 's/{{ MEDIA_URL }}/{% media_url %}\//g'

Upvotes: 3

Andrew White
Andrew White

Reputation: 53516

How about a sed solution...

find ./ -type f -exec sed -i 's/{{ MEDIA_URL }}/{% media_url %}\//g' {} \;

Updated: added /g as per a commenter suggested

Updated: somehow unicode chars got copied in there

Upvotes: 6

Related Questions