Skip to content

Instantly share code, notes, and snippets.

@facelordgists
Created January 30, 2019 22:35
Show Gist options
  • Save facelordgists/4494c4505d4d8d098290917a7f6420c6 to your computer and use it in GitHub Desktop.
Save facelordgists/4494c4505d4d8d098290917a7f6420c6 to your computer and use it in GitHub Desktop.
how to use sed to search and replace strings inside files
#Standalone command
sed -i 's/old string/new string/g' /home/dude/text-file.txt
## use a custom separator
sed -i 's:old/folder/path:new/folder/path:g' /home/dude/text-file.txt
## dry run
sed 's/old string/new string/g' | less
# Combined with find command to search and replace strings inside multiple files
sudo find /var/www/example.com/public_html -type f -exec sed -i 's/old string/new string/g' {} \;
sudo find /var/www/example.com/public_html -type f -exec sed -i 's:old/folder/path:new/folder/path:g' {} \;
sudo find /etc/td-agent/config.d/ -type f -exec sed -i 's:192.168.0.1:192.168.0.2:g' {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment