solairis.com

Command Line Search/Replace

Find files w/find

find . -name vim.html
./notes/vim.html

find . -name 'vi*.html'
./notes/vim.html
			

Search file content w/grep

grep -r '/notes/vim.html' *
contact.html:          <li><a href="/notes/vim.html">Vim</a></li>
index.html:            <li><a href="/notes/vim.html">Vim</a></li>
misc/abc.html:         <li><a href="/notes/vim.html">Vim</a></li>
			

Show only the file names:

grep -rl '/notes/vim.html' *
contact.html
index.html
misc/abc.html
			

Replace w/sed

Replace on STDIN:

echo "Hello World" |sed 's/Hello/Goodbye/g'
Goodbye World
			

Use alternative regex delimiter:

echo '<a href="/notes/vim.html>...' |sed 's#/notes/vim.html#/notes/vim.html#g'
<a href="/notes/vim.html>...
			

Replace in file with -i:

sed -i 's#/notes/vim.html#/notes/vim.html#g' index.html
			

Bulk replacing in multiple files:

for file in `grep -rl '/projects/notes/vim.html' *`; do
    sed -i 's#/projects/notes/vim.html#/notes/vim.html#g' $file
done