Is there a way to grep for a regular expression on an Ubuntu server and then find and replace it with another string? I am using the following command right now, but it doesn't account for actually changing the grepped string:

grep --recursive --ignore-case “string”

If I use awk or sed how would I write it to replace for instance "wavbyte" with "gitship" for all instances where "wavbyte" occurs for my entire server?

  • 1
    You want sed, not grep. Also, you're using "smart" quotes, so grep will search for the literal string “string”, including the quotes. You want "string" (or probably even better, 'string', unless you plan on using parameter expansion in the search term). – Benjamin W. Oct 1 '18 at 0:51
  • 1
    grep isn't used to replace strings. It's used for finding strings. To replace them, you need sed or awk. – Nasir Riley Oct 1 '18 at 0:52
  • So would it be something like – Cody Rutscher Oct 1 '18 at 0:54
  • sed --recursive --ignore-case "string", "replacement-string" – Cody Rutscher Oct 1 '18 at 0:54
  • Related: search and replace using grep (not sed) - however you could use grep --recursive --files-with-matches to construct a list of file names to be passed to sed – steeldriver Oct 1 '18 at 0:58

Try this command

grep -rl "wavbyte" somedir/ | xargs sed -i 's/wavbyte/gitship/g'

You can try find and sed

find /some_path -type f -exec sed -i 's/oldstring/newstring/g' {} \;
  • 1
    Do I replace "matchstring" with the new or old string and what if I want to do it from the root directory of the server? – Cody Rutscher Oct 1 '18 at 1:27
  • For instance if I am replacing "wavbyte" with "gitship" from the root directory of the Ubuntu server would it be: – Cody Rutscher Oct 1 '18 at 1:29
  • find / -type f -exec sed -i 's/wavbyte/gitship/g' {} \; – Cody Rutscher Oct 1 '18 at 1:29
  • Right, but wavbyte is in a lot of different places in the server. How am I going to memorize all of those locations and then go in by hand and change to gitship? – Cody Rutscher Oct 1 '18 at 1:30
  • Let us continue this discussion in chat. – Cody Rutscher Oct 1 '18 at 1:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.