简体   繁体   中英

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?

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

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'

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