Code
find /tmp/ -type f \
\( -name "*.h" \) -o \( -name "*.cpp" \) -o \
\( \! -name "*.bak" \) -exec \
sed -i '1s/^/#include <stdint.h>\n/' {} +
I am not completely sure that this is the right way to go. I write directly to the file without taking temporary files. I want to replace the beginning of the line in .h and .cpp files with but not in .bak files.
How can you make efficient replacement? Note: I am using GNU sed
.
I tried Terdon's command and the insertion is applied to wrong files:
$ cat test.sh
gfind /tmp/ -type f \
\( -name "*.h" -o -name "*.cpp" -o ! -name "*.bak" \) \
-exec gsed -i '1s/^/#include <stdint.h>\n/' {} +
$ sh test.sh
$ cat test.sh
#include <stdint.h>
gfind /tmp/ -type f \
\( -name "*.h" -o -name "*.cpp" -o ! -name "*.bak" \) \
-exec gsed -i '1s/^/#include <stdint.h>\n/' {} +
where all commands are GNU: find and sed.
man find
and the-exec
section? Did you try this and see the error it returned? – terdon Jun 29 '15 at 11:50{} +
to the end or even better to use insteadxargs
, but still something missing. – Léo Léopold Hertz 준영 Jun 29 '15 at 12:01-o
before the!
so it is finding either files ending in.h
or files ending in.cpp
, or files not ending in.bak
. – terdon Jun 29 '15 at 12:33-o
in front of!
and I think everything should be ok. – Léo Léopold Hertz 준영 Jun 29 '15 at 12:35