0

I'm trying to find JPG files in a folder tree to execute the command "" to transform it to PDF.

The problem is that the code I am running leaves the file in the same directory and I want to move it but preserve the structure of the tree of files.

What I've tried so far is:

find . -iname '*.jpg' -exec mogrify -format pdf '{}' \;

This command works perfectly. I want to manipulate "{}" shifting a portion of its content;

{} = ./1/aa.xxx

and I want to convert it to:

{} = ./new/1/aa.pdf

thanks to @mpez0 i can du little of changes an get a solution:

find . -iname '*.jpg' | while read file ; do 
oldfile=${file##*/};
olddir=${file/"$oldfile"/""};
pdfdir=${file/'./''./PDF'};
pdfdir=${pdfdir/"$oldfile"/""};
savedir=${file/'./''./OLD'};
savedir=${savedir/"$oldfile"/""};
newfile=${oldfile/.[Jj][Pp][Gg]/.pdf};
mogrify -format pdf "$file"; 
mkdir -p "$pdfdir";
mkdir -p "$savedir";
mv -- "$olddir$newfile" "$pdfdir$newfile"; 
mv -- "$olddir$oldfile" "$savedir$oldfile"; 
done

what this code do is:
find . -iname '*.jpg' find all the files that end whit jpg and JPG
while read file ; do if the action get ani file save the rout in the variable file
oldfile=${file##*/}; take out the file pat and save te name of the file in the variable oldfile
olddir=${file/"$oldfile"/""}; take out the name of the file to save the pat in olddir
pdfdir=${file/'./''./PDF'}; change ./ to ./pdf and save the new pat in pdfdir
pdfdir=${pdfdir/"$oldfile"/""}; take out the name of the file to save the pdf pat in pdfdir
savedir=${file/'./''./OLD'}; change ./ to ./old and save the new pat in savedir
savedir=${savedir/"$oldfile"/""}; take out the name of the file to save the pdf pat in savedir
newfile=${oldfile/.[Jj][Pp][Gg]/.pdf}; save the name of the new file that will be created in newfile
mogrify -format pdf "$file"; convert the file to pdf
mkdir -p "$pdfdir"; create the directories of the pat pdfdir
mkdir -p "$savedir"; create the directories of the pat savedir
mv -- "$olddir$newfile" "$pdfdir$newfile"; move the new pdf file create to the pdf directorie mv -- "$olddir$oldfile" "$savedir$oldfile"; move the old jpg file to the old directorie done end the proces

I hope this will help others

  • does mogrify have a -o flag for output location? would simplify code a bit. also, what OS are you using? more specifically, what shell? With find, can we assume a Linux shell? – Rich Homolka Dec 19 '14 at 22:07
  • Rich, I'm using bash even in mac as in linux, no mogrify dosen't have it. – Alejandro O'Byrne Dec 19 '14 at 23:25
  • @AlejandroO'Byrne Please post the solution as an answer instead of editing body of your question(You can answer your own question). Even you can accept the answer too, which is worked for you. – αғsнιη Dec 21 '14 at 8:02
1
find . -iname '*.jpg' | while read f ; do 
    new=`echo $f | sed -e 's/\.[Jj][Pp][Gg]$/.pdf/'`; 
    newd=`dirname $f`;
    newd="new/$newd" 
    mogrify -format pdf $f; 
    mv $new $newd; 
done

Of course, I recommend running the script echoing the mogrify and mv commands. If mogrify offers an output option, use that instead of the mv. You may find it simpler to do a find and do all the mogrify transforms, then collect the new .pdfs.

  • thanks whit a little bit change i get the solution from your sugestion. THANKS – Alejandro O'Byrne Dec 20 '14 at 18:26
1

If you use convert instead of mogrify (part of the same suite), your command then becomes:

find . -iname '*.jpg' -exec convert -format pdf '{}' new/'{}'.pdf \;

Unlike mogrify, convert uses does not create an updated file extension, so you will get .jpg.pdf (plus capitalisation) as the new file extension, but that is easy to change with rename:

find new -iname '*.jpg.pdf' -exec rename 's/\.[Jj][Pp][Gg]//' '{}' \;

You can remove the JPEGs afterwards if you don't need to keep them (I hope you don't need that command spelling out).

  • this dosen't work – Alejandro O'Byrne Dec 20 '14 at 18:45
  • What doesn't work? I checked both commands with a single file in a temporary directory. Maybe the directory structure needs to be created in advance, using find . -type d -exec mkdir new/'{}' \;. Note that this will create directories even when there is no JPEG file in it. It would need to be a lot more complex to account for this, but you can always remove empty directories afterwards with find new/ -type d -exec rmdir '{}' \;. – AFH Dec 20 '14 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.