How To Quickly Find And Replace Text Across Multiple Files With One Command

If you need to find and replace the occurrence of a word, phrase, URL or whatever, and it’s in several documents, this can be a really tedious task. If you’re running Mac OS X, Linux, or really any Unix-based operating system, you can use the command line to save you a lot of time and effort.

All you really need is this simple command:

perl -pi -w -e 's/SEARCH_FOR/REPLACE_WITH/g;' *.txt

The search string is what you need to alter. You want to replace SEARCH_FOR with the text you’re searching for and REPLACE_WITH with the text you want to use as a replacement. You’ll also want to change *.txt if you’re working with HTML files (or another type of text file). This command also assumes you’re in the directory you want, so you’ll also need to use cd to change to the directory you want or will have to specify the full path. For example:

perl -pi -w -e 's/stupid/awesome/g;' ~/Desktop/*.txt

The above command will replace all occurrences of “stupid” with “awesome” found in any .txt files on the desktop. Pretty neat!

UNIX: Find and replace text across multiple files [Pressbin.com]


The Cheapest NBN 50 Plans

Here are the cheapest plans available for Australia’s most popular NBN speed tier.

At Lifehacker, we independently select and write about stuff we love and think you'll like too. We have affiliate and advertising partnerships, which means we may collect a share of sales or other compensation from the links on this page. BTW – prices are accurate and items in stock at the time of posting.

Comments


4 responses to “How To Quickly Find And Replace Text Across Multiple Files With One Command”

Leave a Reply