bashshellgnu-findutils

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?


Solution

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