How to Rename/Convert Files in Shell Scripting?

Hi All,

I want to Rename/Convert all the .doc files in to .pdf format.
I am using the following Script. But the final output is not proper.

##########################################
cd /u13/prepaid/ftpdata/INfiles/sap/

for name in `ls .doc`
do
name1=`echo $name | sed -e 's/^\(.
\)\.doc$/\1\.pdf/g'`
mv $name $name1
done

##########################################

Please give me the suggestion to convert files into .pdf format.

Regards
Hanuma

Try this...

for name in `ls -1 *.doc`
do
        mv $name ${name%.doc}.pdf
done

Or even this:

for name in *.doc ...

---------------------- Failed Output-------------------------
bash-2.05$ cat HelloPDF.sh

cd $HOME

for name in `ls -1 *.doc`
do
mv $name ${name%.doc}.pdf
done

bash-2.05$ sh HelloPDF.sh
HelloPDF.sh: bad substitution
------------------ Failed Output---------------------------

Are you sure you're using bash?

$ $0 --version
GNU bash, version 2.03.0(1)-release (sparc-sun-solaris)
Copyright 1998 Free Software Foundation, Inc.
$ f=name.doc
$ echo ${f%doc}pdf
name.pdf

I think you're using the old plain sh:

$ ps -p $$
   PID TTY      TIME CMD
 19211 pts/1    0:00 sh
$ f=name.doc
$ echo ${f%doc}pdf
bad substitution

Try changing the first line of your script:

#! /bin/bash

bash-2.05$ ps -p $$
PID TTY TIME CMD
6934 pts/51 0:00 bash

Still it will give the same error message (: bad substitution)..

Could you post the output of the following command:

bash -c 'x=OK.;echo ${x%.}'

bash-2.05$ bash -c 'x=OK.;echo ${x%.}'
OK
bash-2.05$

So your script is not interpreted by the bash interpreter ...
Try running your script like this:

bash <script_name>

It will give the .pdf file. But it is not in openable format.

At the start of the post "I will write the code. That is also give the same .pdf file"

That is also not in openable format..

I was only trying to explain the syntax error ...
The loop above will not convert - it will only rename - your files :slight_smile:

have a read here Convert MS/Word to PDF for info on converting from doc to pdf using an openoffice macro run from a shell script.