Problem
I use this command to find and change all instances of ‘apple’ in all files in the root of my site with ‘orange’:
find ./ -exec sed -i 's/apple/orange/g' {} \;
But it doesn’t go through sub directories.
What exactly is the issue with this command?
Here are some lines from find./’s output:
./index.php
./header.php
./fpd
./fpd/font
./fpd/font/desktop.ini
./fpd/font/courier.php
./fpd/font/symbol.php
Asked by hd.
Solution #1
To prevent providing directory names to sed, your find should look like this:
find ./ -type f -exec sed -i -e 's/apple/orange/g' {} \;
Answered by jfg956
Solution #2
grep and xargs are better and faster for larger s&r tasks, so for example;
grep -rl 'apples' /dir_to_search_under | xargs sed -i 's/apples/oranges/g'
Answered by Julius
Solution #3
Because there are macOS users reading this (as I did), I used the following code (on 10.14)
egrep -rl '<pattern>' <dir> | xargs -I@ sed -i '' 's/<arg1>/<arg2>/g' @
On macOS, all other responses that use -i and -e do not work.
Source
Answered by pat-s
Solution #4
This was effective for me:
find ./ -type f -exec sed -i '' 's#NEEDLE#REPLACEMENT#' *.php {} \;
Answered by blackdad
Solution #5
grep -e apple your_site_root/**/*.* -s -l | xargs sed -i "" "s|apple|orage|"
Answered by rocLv
Post is based on https://stackoverflow.com/questions/6758963/find-and-replace-with-sed-in-directory-and-sub-directories