CLI search and replace in multiple files

By | March 11, 2010

Here is a quick tip about how to find and replace a text in multiple files, from CLI.

In this example you have some text files (.txt) with urls like “http://example.com, http://another-example.com…”. What you are trying to do is to replace all “http” references with “https”.

You can find all text files using the following command in the directory where the files are located:

find -type f -name *.txt

You can execute a command on the search result using the -exec parameter for the find command:

find -type f -name *.txt -exec my_command

The actual search and replace will be performed using sed command:

find -type f -name *.txt -exec sed -i 's/http/https/' {} \;

Leave a Reply