Blog

Bash Command to Replace a String in a File

If you want to replace a string in a file from the command line (bash), you can use the command sed. As an example, we want to replace PermitRootLogin yes by PermitRootLogin no in /etc/ssh/sshd_config:

sed -i -e 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config

If you want to do an case insensitive replace, you have to add /gi as suffix (instead of /g):

sed -i -e 's/permitrootlogin yes/PermitRootLogin no/gi' /etc/ssh/sshd_config