Score:1

How to find and replace a string in all files on ubuntu including root files with another string

us flag

I am running Ubuntu 20.04 server. It is a fully configured LEMP email and web server. In my super long process of building this, i know I entered my servers IP address on one or many of my configuration and nginx/modsecurity files. The problem is that I am changing IP addresses for the server and i dont remember all of the files that i entered my ip address into for configuration purposes. Most of these configuratin files are in random directories only accessible by root. Most files end with .conf, .bak, .txt, or something else that I cant remember.

The problem is that I am changing IP addresses, so I need to replace my old ip address in all server files with my new IP address.

My question is, what command can i use to tell ubuntu to search all strings in all files for my old IP address, and replace them with my new IP address? This would save me the trouble of going through every file manually, and also ensure that I dont make a mistake and miss a file, thus having a server with some files with my old ip address and some files with my new ip address.

I dont know grep very well but i would imagine the command would include a grep command. Any help is much appreciated.

Marco avatar
br flag
First, as admin you need to have some kind of "logbook" where you write down all changes!! Otherwise you will get in trouble sooner or later (as you are now). The option for grep is `-r` or completely: `grep -r "192.168.0.2" /etc` at least to find the files containing the ip. Better make it manual, otherwise your system might break and you are lost.
cn flag
Please do not do this and let an actual admin of that server do this. You do not use a comet from outer space to squat a fly that annoys you. Find out what software is running, find its conf file and alter that. And make documentation of what you change. Your method is a direct violation of the safety of your machine. 1 mistake and it is dead beyond repair.
Score:1
it flag

You're unclear in what you ask. Here's how to search each and every file on your system for your old IP. Replace the / with another directory to limit the search.

sudo find / -type f -print0 |\
     xargs -0 -r sudo grep -F -l 'oldIP'

If you're happy with that list of files, do almost the same thing:

sudo find / -type f -print0 |\
     xargs -0 -r sudo grep -F -l --null 'oldIP' |\
     xargs -0 -r sudo perl -pi~ -e 's/oldIP/newIP/g' 

This will leave your original, unmodified files as filename~, for recovery, just in case. You should backslash-escape the dots in oldIP (but not newIP) in the perl expression, e.g. -e 's/192\.168\.97\.2/192.168.14.17/g'.

DanRan avatar
us flag
Thank you very much for this answer. This is what I was looking for my friend!
waltinator avatar
it flag
@DanRan Please click the checkmark to "accept" my answer, if it solved your problem, and might help others.
DanRan avatar
us flag
done and done. checkmarked.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.