Find and replace with sed in directory and sub directories

hd. picture hd. · Jul 20, 2011 · Viewed 260.4k times · Source

I run this command to find and replace all occurrences of 'apple' with 'orange' in all files in root of my site:

find ./ -exec sed -i 's/apple/orange/g' {} \;

But it doesn't go through sub directories.

What is wrong with this command?

Here are some lines of output of find ./:

./index.php
./header.php
./fpd
./fpd/font
./fpd/font/desktop.ini
./fpd/font/courier.php
./fpd/font/symbol.php

Answer

jfg956 picture jfg956 · Jul 20, 2011

Your find should look like that to avoid sending directory names to sed:

find ./ -type f -exec sed -i -e 's/apple/orange/g' {} \;