CLI search and replace in multiple files

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/' {} \;

About Cristian Radulescu

PHP // MySql // Linux // Android
This entry was posted in Linux and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>