Replacing a word in Linux files

Once, when replacing the network adapter in the server, I needed to change its old name in the netplan configuration, that is, replace several hundred words in the file, so I came up with the command:

cd /etc/netplan/
find -type f -name 50-cloud-init.yaml -exec sed -i -r 's/link: ens4/link: ens5/g' {} \;

You can also specify from which line to which to replace the word, for example, from line 10 to line 150:

:10,150s/word1/word2/g

See also my articles:
Using the vim text editor
Searching for text using grep

Leave a comment

Leave a Reply