Friday, January 6, 2012

ps2pdf to all ps files in current directory

for x in $(ls *.ps); do echo $x; ps2pdf $x $(echo $x|sed -e 's/ps$/pdf/');done

or more efficiently according to Yarko's comment

for x in *.ps; do
echo $x;
ps2pdf $x ${x/%ps/pdf};
done


1 comment:

Yarko said...

Haky -

This is a little too complicated:
for x in $(ls *.ps); do echo $x; ps2pdf $x $(echo $x|sed -e 's/ps$/pdf/');done

The key is to speak with no unnecessary words:

Every $(...) expression runs a new bash shell as a child - both are unnecessary.

ls *.ps - runs a program with the shell expanding wildcard names to a list of matching names; for you the directory-listing program serves no purpose;

The second parenthesized expression used to form the output file-name is launching the powerful sed program to do the simplest of substitutions.

The simple substitution you need can be readily done by the shell (bash, these days, if you're either on mac or linux).

Here's the simpler, faster, easier to read, more direct version:

for x in *.ps; do
echo $x;
ps2pdf $x ${x/%ps/pdf};
done

Search for the section on "Parameter Expansion" in the bash man page (man bash) for details about substitution syntax.

Contributors

google