-1

This question already has an answer here:

Say I have an IP address + domain (1.1.1.1/example.com) appearing in many different files which are prevalent inside different files throughout the system and I want to change it's IP to something else (say, example.com without the IP), in a fast way.

How could I search and replace in the entire system from the command line?

Update for Terdon:

After reading the comments (and after the first version of the command in Dimitar's answer might have damaged my system because lack of escaping), it seems best just to find the files and change manually or target accordingly. For start After I re installed the server from backup I tried to change manually and to locate the files I did for example:

sudo grep -rnw '/' -e "138.68.83.195"
sudo grep -Ril "138.68.83.195" /
sudo find / -type f -exec grep -H '138.68.83.195' \{\} \;
sudo find / -type f -exec grep -Fq '138.68.83.195' {} \; -a -ls

(In some variations I added /example.com right after the IP).

I tried these commands to find a string in all files. I want to find all files with this string. Yet, all of them return:

grep: /proc/sys/fs/binfmt_misc/register: Invalid argument
grep: /proc/sys/net/ipv4/route/flush: Permission denied
grep: /proc/sys/net/ipv6/conf/all/stable_secret: Input/output error
grep: /proc/sys/net/ipv6/conf/default/stable_secret: Input/output error
grep: /proc/sys/net/ipv6/conf/ens4/stable_secret: Input/output error
grep: /proc/sys/net/ipv6/conf/eth0/stable_secret: Input/output error
grep: /proc/sys/net/ipv6/conf/lo/stable_secret: Input/output error
grep: /proc/sys/net/ipv6/route/flush: Permission denied
grep: /proc/sys/vm/compact_memory: Permission denied

marked as duplicate by Ipor Sircer, Jeff Schaller, user181255, GAD3R, Kusalananda Jan 9 '17 at 15:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    wow. doing that in the whole filesystem, not only someplace like /etc but /home, /bin, /usr/bin, /dev, /proc, /sys etc. That would seem to have a stunning risk of not ending well – infixed Jan 9 '17 at 14:50
  • @infixed you're absolutely right. – 13dimitar Jan 9 '17 at 15:12
  • Well, than what alternative will you suggest in such a case? – JohnDoea Jan 9 '17 at 15:17
  • 1
    find / -type f -exec grep -H 'Ipanddomain' \{\} \; > possiblefiles.txt will give you an idea of what needs to be changed. Review that. If you change the option of grep to -l and you will get a list of bare names that you can edit with a text editor to have only the files needed. Then you could use xargs with that list to run the sed command, Or just do it manually if its only a few places – infixed Jan 9 '17 at 16:36
  • 1
    I don't know if upper/lower cases may be involved in your domain, but the -i option could be used to ignore case – infixed Jan 9 '17 at 16:40
2

You can do it with

find / -type f -exec sed -i.bak 's/IP_and_domain/domain_only/g' {} \;

Note that you will need to use a different delimiter (e.g. |) since what you are looking for contains a backslash:

find / -type f -exec sed -i.bak 's|1\.1\.1\.1/domain|domain|g' {} \;

You might also want to redirect all stderr to some temporary file, just to go minimal and less verbose.

The -i.bak will create a backup file (with the .bak extension) of any file you edit. Please note that searching through the entire system for this is very dangerous. It is also pointless. You are almost certainly only interested in specific directories, so only search through those. Also, before running the command above, identify the files that will be modified and make sure those are the ones you want and only the ones you want:

find / -type f -exec grep -Fq '1.1.1.1/domain' {} \; -a -ls
  • 1
    That's not that simple. The replaced string should be anchored to the start of a word, the dots should be escaped, the slashes must not be used. Else it won't work or worse, disaster might ensue. With OP's example, s|1.1.1.1/example.com|example.com|g, would change 33.44.101.191/example.com to 33.44.example.com. Not exactly what is expected. – xhienne Jan 9 '17 at 17:08
  • Can you please edit the answer accordingly (and also check my edit on the way) ? I'm not sure I understand and the current example there didn't work for me. – JohnDoea Jan 10 '17 at 21:22
  • @Benia please see edited question. You really, really don't want to blindly run replacement commands on all files on your system. First find your target directories, at least. – terdon Jan 11 '17 at 9:56
  • Indeed I don't. I tried to ran several commands to find files so I could change each one with Nano manually (or at least better target everything, as you suggest) but all commands printed stderr and didn't show any files. – JohnDoea Jan 11 '17 at 10:05
  • Thank you dearly Terdon for showing a way to find it to change manually which is indeed safer and that's what I had to done in the first place. I tried 4 commands for this, including the one you presented but all just return stderr and dodesn't find the file. I've update the question with an example. I will try to solve it in the coming hours but I'll very much appreciate any clue you might want to give me. – JohnDoea Jan 11 '17 at 10:34

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