Dickson Afful
Dickson Afful

Reputation: 838

Write to all file matches in find command

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?

Upvotes: 1

Views: 521

Answers (1)

Socowi
Socowi

Reputation: 27330

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"' {} \;

Upvotes: 2

Related Questions