I have been looking for an answer to this problem for a while: how can I use find append text to all hits? My original method was something like
sudo find / -type f -name "*.py" -exec cat somecode.txt >> {} \;
although all that did was create a file name {} filled with the contents of somecode.txt repeated one for each hit. I then tried running the command as
sudo find / -type f -name "*.py" -exec echo somecode.txt >> {} \;
although that had the same effect, just this time {} was filled with the string somecode.txt repeated once f or every hit. Is there any other way to accomplish this, or am I just missing something fundemental?
My intended goal is for the contents of somecode.txt to be appended to every python file in the system.


1 Answer 1


Just -exec a shell for each argument:

find . -type f -name "*.py" -exec sh -c 'cat somecode.txt >> "$1"' -- {} \;

(Starting out without sudo and in a place other than / might be a good idea).

If you want to be extra efficient, you might want to try something like:

find . -type f -name "*.py" -exec sh -c '< somecode.txt tee -a "$@"' -- {} +

You must log in to answer this question.