November 18

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’ {} +

Copyright 2021. All rights reserved.

Posted November 18, 2021 by Timothy Conrad in category "Linux

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B