Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

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

When I want to process a bunch of tex files, I use

for f in *.tex; do latex "$f" ; done;

What do I do when I need to redirect output to another file, like in case of catdvi?

The following does not work:

for f in *.dvi ; do catdvi -e 1 -U $f  > "${f%.dvi}.txt"; done;

What am I doing wrong?

share|improve this question
up vote 2 down vote accepted

You forgot to quote $f, so the filename probably got wordsplit due to whitespace in the filename. I also suggest using ./*.dvi as this avoid problems if a filename starts with a - (which will make most commands treat it as a set of options instead of a filename)

for f in ./*.dvi; do catdvi -e 1 -U "$f" > "${f%.dvi}.txt"; done

Or perhaps combine the two loops

for f in ./*.tex; do 
    latex "$f" &&
    catdvi -e 1 -U "$base.dvi" > "$base.txt";
share|improve this answer
Perfect! Solved the problem! Thanks!! – deshmukh Jul 23 '12 at 10:49

Test it by reducing the complexity of the command. Try this first:

for f in *.dvi ; do catdvi -e 1 -U $f >> catdvi.txt; done;

If that works, then the problem is with your dynamic file-name-creation code. Perhaps this would work:

for f in *.dvi ; do catdvi -e 1 -U $f > "${f%.dvi.txt}"; done;

What is the f% trying to do? Should that be $f instead?

for f in *.dvi ; do catdvi -e 1 -U $f > "${$f.dvi.txt}"; done;

share|improve this answer
None of the commands work! On the first and second command, the error is: Expecting zero one 1 arguments. Got 9, etc. On the third command, the error is Bad substitution. Hey, I thought this would be simple to do. It is turning out to be quite complex! – deshmukh Jul 21 '12 at 7:28

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.