Search code examples
macossedfind

Find and sed together fail in macOS


This command finds files if I remove from exec onwards.

find src -name "*.tsx" -o -name "*.ts" -exec sed -i "" "s/LayerSelector/LayerRenderer/g" {} \;

The LayerSelector is indeed in most of those files, and the -i should replace in place.

However, nothing happens. No changes in files, no message.

What is wrong with this simple command ?


However, this works fine:

sed -i "" "s/LayerSelector/LayerRenderer/g" src/**/*.tsx

Solution

  • You are missing parentheses around the -o clause:

    find src \( -name "*.tsx" -o -name "*.ts" \) -exec sed -i "" "s/LayerSelector/LayerRenderer/g" {} \;
    

    Without the parentheses, due to operator precedence, this:

    find src -name "*.tsx" -o -name "*.ts" -exec sed -i "" "s/LayerSelector/LayerRenderer/g" {} \;
    

    is the same as this (which is probably not what you want, because it does nothing to "*.tsx" files):

    find src \( -name "*.tsx" \) -o \( -name "*.ts" -exec sed -i "" "s/LayerSelector/LayerRenderer/g" {} \; \)
    

    See also: