☰ Categories

Linux: Finding text within multiple files in multiple directories and replacing the text

I like to start these type of recursive runs in the root directory of the search. So in our test case I would do the following:

Eg. Find the port number 8080 in multiple .xml files and replace it with the port number 15001.

  1. cd /path/rootdirectoryofsearch

*note – Change which files get examined by changing the extension type:

  1. Then run the command
    With Backup .mybackup
    find . -type f -name “*.xml” -exec sed -i.mybackup -e ‘s/8080/15001/g’ {} +
  2. Delete backups:
    find . -name “*.mybackup” -type f – delete
  3. With Backup
    find . -type f -name “*.xml” -exec sed -i” ‘s/8080/15001/g’ {} +