Linux Tip: Search & Replace text in multiple files recursively

Here is how to find and replace text across multiple files recursively.

The following command will recursively find all files with extension .html starting with the current folder. In each file, the text apple will be replaced with orange.

find . -name "*.html" -print | xargs sed -i 's/apple/orange/g'

Linux Tip: Removing blank lines from a text file

Method 1: Using grep

$ grep -v '^$' infile.txt > outfile.txt

Method 2: Using sed

$ sed '/^$/d' infile.txt > outfile.txt

To remove blank lines from multiple files a script like the one below can be used

#!/bin/sh
files="/somefolder/*.txt"
for f in $files
do
  sed '/^$/d' $f > $f.tmp
  mv  $f.tmp $f
done