Operating system Linux How to replace same text in multiple files in linux?
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149

How to replace same text in multiple files in linux?

If you have text you want to replace in multiple locations, how to replace same text with new text you want? Well, to do this, there are several ways. To replace the text Windows with Linux in all files in current directory called test[something] you can run the following commands:

perl -i -pe 's/Windows/Linux/;' test*

To replace the text Windows with Linux in all text files in current directory and down you can run like this:

find . -name '*.txt' -print | xargs perl -pi -e's/Windows/Linux/ig' *.txt

Or if you prefer this will also work, but only on regular files:

find -type f -name '*.txt' -print0 | xargs --null perl -pi -e 's/Windows/Linux/'

Ok, if this works, it will saves a lot of time for you and has a high guru rating!

Let us remember these commands:

perl -i -pe 's/Windows/Linux/;' test*
find . -name '*.txt' -print | xargs perl -pi -e's/Windows/Linux/ig' *.txt
find -type f -name '*.txt' -print0 | xargs --null perl -pi -e 's/Windows/Linux/'

You now see that, in fact, we use only two easy command line "perl" and "find" to achieve our purpose to replace same text in different files.


Operating system - Linux