views:

54

answers:

4

Greetings,

I've got a directory with a list of pdfs in it:

file1.pdf, file2.pdf, morestuff.pdf ... etc.

I want to convert these pdfs to pngs, ie

file1.png, file2.png, morestuff.png ... etc.

The basic command is,

convert from to,

But I'm having trouble getting convert to rename to the same file name. The obvious 'I wish it worked this way' is

convert *.pdf *.png

But clearly that doesn't work. My thought process is that I should utilize regular expression grouping here, to say somethink like

convert (*).pdf %1.png

but that clearly isn't the right syntax. I'm wondering what the correct syntax is, and whether there's a better approach (that doesn't require jumping into perl or python) that I'm ignoring.

Thanks!

+3  A: 
for f in *.pdf
do
  convert "$f" "${f%.pdf}.png"
done
Ignacio Vazquez-Abrams
That appears to work, thank you! Can you elaborate on how/why?
AlexeyMK
It uses parameter substitution to cut away the extension, then replaces it with its own. http://tldp.org/LDP/abs/html/parameter-substitution.html
Ignacio Vazquez-Abrams
+4  A: 
for files in *.pdf 
do  
   if [ -f "$files" ];then
      convert "$files" "${files%.pdf}.png"
   fi
done

if you need to do it recursively,

find /path -type f -iname "*.pdf" | while read -r FILE
do
   convert "$FILE" "${FILE%.pdf}.png"
done
ghostdog74
Internet surfer: If you're looking for a one-line solution, see below.
AlexeyMK
+1  A: 
ephemient
+1  A: 
ls *.pdf | sed 's/\"/\\"/;s/^\(.*\).pdf$/convert "&" "\1.png"/' | bash
What if the filenames contain backslashes or quotation marks?
ephemient
have you tried the command? it works fine with backslash or quotes.
`touch \".pdf` or heck, to be evil, `touch '$(rm -rf /).pdf'`
ephemient
you won't be able to create `touch '$(rm -rf /).pdf'` because `/` is illegal for file name. As for quotes, its a matter of escaping.
way to get it to work inline. File names didn't (in this case) contain any potentially worrysome characters.
AlexeyMK