Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

You can use Vim in Ex mode:

find -name '*.txt' -exec ex -sc '%s/OLD/NEW/g|x' {} ';'
  1. % select all lines

  2. s substitute

  3. g global replace

  4. x save and close

share|improve this answer

Your Answer


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.