General
cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to convert all .ps files in one directory to .pdf

Andrew Kaplan
Super Advisor

Trying to convert all .ps files in one directory to .pdf

Hi there --

I am trying to get ps2pdf to work within a script so that it will convert all .ps files that are in a directory, to .pdf format.

What would the command syntax be in order for me to accomplish this? Thanks.
A Journey In The Quest Of Knowledge
7 REPLIES
Matti_Kurkela
Honored Contributor

Re: Trying to convert all .ps files in one directory to .pdf

for i in *.ps; do ps2pdf $i; done

Add ps2pdf options according to your taste.

MK
MK
Andrew Kaplan
Super Advisor

Re: Trying to convert all .ps files in one directory to .pdf

Hi there --

Thanks for your reply. I inserted the command syntax in question, but the output I got was the following:

Usage: ps2pdfwr [options...] (input.[e]ps|-) [output.pdf|-]
Usage: ps2pdfwr [options...] (input.[e]ps|-) [output.pdf|-]
Usage: ps2pdfwr [options...] (input.[e]ps|-) [output.pdf|-]
Usage: ps2pdfwr [options...] (input.[e]ps|-) [output.pdf|-]
Usage: ps2pdfwr [options...] (input.[e]ps|-) [output.pdf|-]
Usage: ps2pdfwr [options...] (input.[e]ps|-) [output.pdf|-]
Usage: ps2pdfwr [options...] (input.[e]ps|-) [output.pdf|-]

What am I missing here? Thanks.
A Journey In The Quest Of Knowledge
Steven Schweda
Honored Contributor

Re: Trying to convert all .ps files in one directory to .pdf

> What am I missing here?

To start...

1. A description of a ps2pdf command which
works as you'd like on one file.

2. A useful description of the actual
command(s) which failed.


> [...] I inserted the command syntax in
> question [...]

Not a useful description of your starting
point, or of what produced the undesired
output. That new-fangled copy+paste
technology makes it pretty easy to show your
actual commands with their actual output,
all of which is generally superior to vague
descriptions and interpretations.
Andrew Kaplan
Super Advisor

Re: Trying to convert all .ps files in one directory to .pdf

Hi there --

Sorry about the vagueness in my postings. When I run the ps2pdf command manually on one file, the syntax is similar to the following:

/usr/bin/ps2pdf 'Course 1.Plan 1.ISOCENTER_SUMMARY.ps'

The pdf file is successfully created.

The command syntax that I used in the script which failed are as follows:

for i in *.ps;
do ps2pdf $i;
done

Seriously, does that clarify things?
A Journey In The Quest Of Knowledge
Steven Schweda
Honored Contributor

Re: Trying to convert all .ps files in one directory to .pdf

> Seriously, does that clarify things?

Some. Perhaps enough.

What are the file names? "ls -l"?

> /usr/bin/ps2pdf 'Course 1.Plan 1.ISOCENTER_SUMMARY.ps'

> do ps2pdf $i;

Not really the same, are they? One has a
full path to "ps2pdf". One has a quoted
argument. (Why? Funny file names which need
quotation?)

A little debug output might be informative.
For example:

do echo ">${i}<" ; ps2pdf $i

Personally, I tend to avoid things like
"*.ps" in a shell script. I assume that the
shell will get a too-long line, or some file
name with embedded spaces will get broken
apart, or some other disaster will befall it.
"find" is harder to use, but also harder to
bewilder.

Oh, look. What's that between "Course" and
"1"? Could it be a space?

Something like
ps2pdf "$i"
might be helpful.

See what I mean about actual stuff instead of
descriptions of stuff?
Dennis Handly
Acclaimed Contributor

Re: Trying to convert all .ps files in one directory to .pdf

As Steven said, having spaces in filenames will hose you. I don't think you can use a for-loop and you should use find(1):
find . -name "*.ps" | while read file; do
ps2pdf "$file"
done
(This will descend into subdirectories.)
Steven Schweda
Honored Contributor

Re: Trying to convert all .ps files in one directory to .pdf

> [...] having spaces in filenames will hose
> you.

If you're careless.

> I don't think you can use a for-loop
> and you should use find(1):

Actually, "for" seems to be smarter than I'd
expect:

dy # for i in a* ; do echo "$i" ; done
a b c
a b.c d
dy # uname -a
HP-UX dy B.11.11 U 9000/785 2012616114 unlimited-user license

Could depend on your shell, of course.
UNIX(-like) shells don't make it particularly
easy to handle spaced file names, but it can
often be done, if one pays attention to
details.

VMS (on an ODS5 file system) does things
differently, of course:

bash$ ls -l a*b*
-rw-r----- 1 SMS 40 5 Jan 26 2009 a b c d
-rwxr-x--- 1 SMS 40 8 Apr 18 2008 a.b.c.d
-rwxr-x--- 1 SMS 40 3654 May 17 2006 a.b.c.d.e
-rw-r----- 1 SMS 40 0 Jul 28 01:02 a:b

alp $ dire /date /prot /size a*b*

Directory DKA100:[sms]

a^_b^_c^_d.;1 1 26-JAN-2009 15:39:50.55 (RWD,RWD,R,)
a^.b^.c.d;1 1 18-APR-2008 22:04:10.44 (RWED,RWED,RE,)
a^.b^.c^.d.e;1 8 17-MAY-2006 15:20:43.38 (RWED,RWED,RE,)
a^:b.;1 0 28-JUL-2009 01:02:35.88 (RWD,RWD,R,)

Total of 4 files, 10 blocks.

You can use literal spaces with the C RTL,
and you can actually use "^ " on the command
line, but that caret-escape mechanism, deftly
(?) avoids literal spaces on the command
line.

bash$ ls -l a\ b*
-rw-r----- 1 SMS 40 5 Jan 26 2009 a b c d

alp $ dire /date /prot /size a^ b*

Directory DKA100:[sms]

a^_b^_c^_d.;1 1 26-JAN-2009 15:39:50.55 (RWD,RWD,R,)

Total of 1 file, 1 block.


You can avoid escaping all the non-last dots,
and some other things, too:

alp $ dire /date /prot /size a.b.c.d.e

Directory DKA100:[sms]

a^.b^.c^.d.e;1 8 17-MAY-2006 15:20:43.38 (RWED,RWED,RE,)

Total of 1 file, 8 blocks.

But there are some details there, also.
Everything's complicated.