3

I'm trying to recursively find files of multiple types, and replace a certain string in each of the files found. But when I run the script, it only finds files of one single type.

The command line I'm using is this

find . -name '*.tcl' -o -name '*.itcl' -o -name '*.db' -exec sed -i 's/abc/cba/g' {} +

Every time I run the command above, it only finds files of type .db. When I run it with a single file type, it works fine.

doubleDown
  • 8,048
  • 1
  • 32
  • 48
Jean-Luc Nacif Coelho
  • 1,006
  • 3
  • 14
  • 30

3 Answers3

6

You need to group the -names * part with () like that :

Moreover, it's better (from my experience) to run sed -i only on files that match the pattern, so :

find . \( -name '*.tcl' -o -name '*.itcl' -o -name '*.db' \) -exec sed -i '/abc/s/abc/cba/g' {} +
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

Below command will search for either txt or log files and replace the source string with the target string.

 find . -name "*.txt" -o -name "*.log"|xargs perl -pe 's/source/target/g'

you can do the replace ment by adding an -i in the perl statement as below:

find . -name "*.txt" -o -name "*.log"|xargs perl -pi -e 's/source/target/g'
Vijay
  • 65,327
  • 90
  • 227
  • 319
0

You can do:

find . -regex ".*\.\(tcl\|itcl\|db\)" -exec sed -i 's/abc/cba/g' {} +

This is more compact, and you do not have to mess around with OR and grouping the logical operations.

Sasha Pachev
  • 5,162
  • 3
  • 20
  • 20