I'm having some trouble doing something that should be fairly simple. Here is how I imagine it would work:

find . -name *.txt -exec sed 's/oldtext/newtext/g' {} > tmp \\; mv tmp {} \;

Unfortunately I can't sed a file and dump to itself. But I can't figure out how to dump to a temp file and then immediately mv that file. I know \\; is wrong but I can't figure out how to run one command followed by the other.

link|improve this question

77% accept rate
feedback

1 Answer

up vote 0 down vote accepted

See the -i option in man sed.

-i[SUFFIX], --in-place[=SUFFIX]

edit files in place (makes backup if extension supplied)

Less useful but relatedly, you can also string multiple -execs together; they're executed left to right.

tmp=$(tempfile)
find . -name '*.txt' -exec sed 's/oldtext/newtext/g' '{}' > $tmp \; -exec mv $tmp '{}' \;

(Note the quoting of *.txt, {} and the escaping of ; to not have them interpreted by your shell.)

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.