Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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