1

I want to echo some text into all file matches with the find command in linux shell. I tried this" find . -type f -name "file*.txt" -exec echo "some text" > - '{}' ';' But this does not work.Is there anyway I can fix this?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Dickson Afful
  • 708
  • 1
  • 6
  • 20

1 Answers1

2

Your command did not work because > is interpreted by shells like sh or bash. When using find -exec echo only echo is started. There is no shell that could interpret the > as redirection.

Note that > overwrites files. You probably wanted to append. Use >> to do so.

find . -type f -name 'file*.txt' -exec sh -c 'echo "some text" >> "$0"' {} \;
Socowi
  • 25,550
  • 3
  • 32
  • 54