Today I Learned: 03/11/2021 - Modifying all the files in subdirectories with a common element in their name
I recently added a new domain to an nginx config file templating system. The easiest way to do this was to copy all the subdirectories named for a previous domain with the same settings to new subdirectories named for the new domain. I then however needed to replace all references to the original domain in the files in those subdirectories. I had to remind myself how to do this with find and sed:
find . -path "*NEW*" -type f | xargs sed -i "s|OLD|NEW|g"
The find command’s -path argument will return all file and directory paths with the NEW string in them. The -type option restricts the returned set to just regular files. Piping those into xargs and sed will replace the OLD string with the NEW one on each of the find command’s file paths and save them in place with the -i flag.