I have a directory with several subdirectories that all contain text files. In these text files, there is a certain word that occurs over and over again, and I want to replace every instance.
My Question – How would I accomplish this from a linux commandline? I don't want to gedit every file and do a manual search/replace.
(For a bonus – is there a way to do this in a case-insensitive way?)
Best Answer
You could use
find
andperl
(viaxargs
):or if you are already in the right directory
and if you only want to replace in, say, html files:
You don't have to use perl of course, any tool that performs search and replace operations will do, and not doubt there are several available that are less resource hungry than starting perl (which is this case is a sledge-hammer to crack a nut, but it is an example I've had stored for ages and I've no reason to find a more efficient version). My original source for the "trick" is http://oreilly.com/pub/h/73
You can use all the options in which-ever tool you choose, so in this case the full power of Perl's regular expressions. The page linked to above has more examples, including how to search in a case in-sensitive manner.