LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2007, 01:53 PM   #1
mfcarroll
LQ Newbie
 
Registered: Mar 2004
Location: Vancouver, Canada
Distribution: debian testing / ubuntu
Posts: 11

Rep: Reputation: 0
Question find -exec syntax: bug in find?


I'm writing a small bash script to convert a pdf into a series of png images (one for each page of the pdf). This works almost perfectly:

Code:
#!/bin/bash
OUT=./pdf_png_convert/
mkdir $OUT
pdftoppm -r 300 $1 $OUT`basename $1 .pdf`
exit
find $OUT -iname *.PPM -exec convert '{}' `basename '{}' .ppm`.png \;
find $OUT -iname *.PPM -exec rm '{}' \;
mv $OUT/* .
However, the reduction of the final image name to .png using basename doesn't work. I'm left with .ppm.png files.

I know I can work around this quite easily by doing the convert then rename afterwards to fix the extensions, but this seems like it might be a bug in find. I reduced this to the following test case:

Code:
$ find -iname *.PPM -exec basename {} .ppm \;
test-000001
vs.

Code:
$ find -iname *.PPM -exec echo `basename {} .ppm` \;
./test-000001.ppm
I'm no bash guru, but is this a bug in find, or just my understanding of the -exec syntax? Any thoughts?

Thanks
Matthew
 
Old 06-20-2007, 02:32 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
The `basename '{}'` is done by bash before the execution of the find; basename returns the string {}, and bash then substitutes {} for `basename '{}' .ppm`.

It took me a few minutes to see this.
 
Old 06-20-2007, 02:42 PM   #3
mfcarroll
LQ Newbie
 
Registered: Mar 2004
Location: Vancouver, Canada
Distribution: debian testing / ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
Of course... silly me for not seeing that. Thanks.

Code:
$ echo `basename {} .ppm`
{}
So, the question, now, is "is there any way to do that in a single line" - that kind of thing bugs me; I feel like there must be an elegant way to do that as part of the -exec. Maybe there's a bash guru out there with an answer?
 
Old 06-21-2007, 01:56 PM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
If you're willing to have a rather long "single line", and you're willing to forego the -exec, how about this?

Code:
( find $OUT -iname '*.PPM' > temp1) ; (sed -e 's/...$/png/' temp1 > temp2) ; (sed -e 's/^/convert /' temp1 > temp3 ) ; (paste temp3 temp2 > temp4); . temp4; rm temp1 temp2 temp3 temp4
 
Old 06-21-2007, 02:15 PM   #5
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
Code:
... -exec sh -c 'convert {} $(basename {} .ppm).png' \;
Or

Code:
for f in $(find $OUT -iname *.PPM); do
  tmp=${f##*/}
  convert $f ${tmp%.*}.png && rm $f
done
 
Old 06-21-2007, 07:13 PM   #6
mfcarroll
LQ Newbie
 
Registered: Mar 2004
Location: Vancouver, Canada
Distribution: debian testing / ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
Talking

Thanks everyone. dawkcid's solution seems the cleanest - I've done that in bash that I understand. Here's the final script:

Code:
#!/bin/bash
OUT=./pdf_png_convert/
mkdir $OUT
pdftoppm -r 300 $1 $OUT`basename $1 .pdf`
for f in `find $OUT -iname *.PPM`; do
   BASE=`basename $f .ppm`
   convert $f $OUT/$BASE.png && rm $f
done
mv $OUT/* .
rmdir $OUT
 
  


Reply

Tags
basename, bash, exec, find



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find: missing argument to `-exec' pavodive Linux - Newbie 4 10-05-2009 02:24 PM
find -exec help dc144 Linux - Newbie 3 10-12-2006 02:14 AM
Help sought with 'find -exec' drjones69 Linux - Software 2 04-27-2005 04:02 AM
find -exec question eantoranz Linux - General 1 06-26-2004 10:57 AM
find -exec cricbk Linux - Newbie 4 01-05-2004 07:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration