I want to prepend recursively all files ending with .ts with this text: export {}; and I need to exclude at least the folder node_modules.

I tried:

find . f ! -path '*/node_modules/*' -iname "*.ts" -exec sed -e '1 i\export {}' \;

Result: for each file I get: sed: 1: "1 i\export ./src/query/ ...": extra characters after \ at the end of i command

Update: I installed gnu-sed on macOS and tried using gsed as well. But it doesn't seem to work - it seems to run infinitely.


Due to:

The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a \) or quoted to protect them from expansion by the shell.

Use the following approach:

find . -type f ! -path '*/node_modules/*' -iname "*.ts" -exec sed -i '1 i\export\{\}' {} \;

-i, --in-place - sed option for editing files in place

  • When using gsed instead of sed (because of macOS) it did work, thanks. – Alexander Zeitler Jan 13 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.