Searching Through Files - Finding A Particular String And Updating It
Main command: grep -rnw "xA" -e "xB" | xargs sed -i 's/xC/xD/'
- grep – Obvious, search to find results.
- -rnw – Search all directories after string definition.
- “xA” – Location to search.
- “xB” – String to search. Has to be in quotations!
- | - Pipe, put results from grep into sed.
- xargs – Call on argument from result.
- sed – Replace.
- 's/xC/xD/' – Replace string xC with xD.
If you see on the example below, you would use the grep command up to the pipe symbol first without -l, then add it in afterwards. This is used after searching for strings. The letter L is for grep’ing just the directory location where it finds the result and not actually showing file contents.
SED WILL NOT work if you do not add -l into the -rlnw. It has to require the absolute paths and not anything else it outputs.
An Example
Searching through LuckPerms configs in all servers to replace connection timeout string.
grep -rlnw "connection-timeout: 5000" */plugins/LuckPerms | xargs sed -i 's/connection-timeout: 5000/connection-timeout: 20000/'