Buscar y reemplazar con sed en directorios y subdirectorios


Corro este comando para buscar y reemplazar todas las ocurrencias de ' apple 'con' orange ' en todos los archivos en la raíz de mi sitio:

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

Pero no pasa por subdirectorios.

¿Qué tiene de malo este comando?

Editado: aquí hay algunas líneas de salida de find ./ comando:

./index.php
./header.php
./fpd
./fpd/font
./fpd/font/desktop.ini
./fpd/font/courier.php
./fpd/font/symbol.php
Author: hd., 2011-07-20

6 answers

Su find debería verse así para evitar enviar nombres de directorio a sed:

find ./ -type f -exec sed -i -e 's/apple/orange/g' {} \;
 302
Author: jfg956,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-19 02:35:38

Para tareas más grandes de s & r es mejor y más rápido usar grep y xargs, así que, por ejemplo;

grep -rl 'apples' /dir_to_search_under | xargs sed -i 's/apples/oranges/g'
 56
Author: Julius,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-25 10:55:11

Esto funcionó para mí:

find ./ -type f -exec sed -i '' 's#NEEDLE#REPLACEMENT#' *.php {} \;
 4
Author: blackdad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-04 10:29:08
grep -e apple your_site_root/**/*.* -s -l | xargs sed -i "" "s|apple|orage|"
 1
Author: rocLv,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-26 06:11:11

Creo que podemos hacer esto con una línea simple comando

for i in `grep -rl eth0 . 2> /dev/null`; do sed -i ‘s/eth0/eth1/’ $i; done

Refiérase a esta página.

 1
Author: Sukrant,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-01 19:14:16

En linuxOS:

sed -i 's/textSerch/textReplace/g' namefile

Si" sed " no funciona intente:

perl -i -pe 's/textSerch/textReplace/g' namefile
 -3
Author: ฉัตรชัย สิทธิวงค์,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-07 03:34:11