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?
EDIT:
My intended goal is for the contents of somecode.txt
to be appended to every python file in the system.
Asked
Active
Viewed 7,822 times
2
-
See also How to prepend a particular text file contents to every text file in a directory and its subdirectories? – don_crissti Dec 04 '15 at 03:14
1 Answers
5
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 "$@"' -- {} +

Petr Skocik
- 28,816