Register
It is currently Wed Oct 01, 2014 2:16 pm

extract and rename .jpg file from each .zip using for loop


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Sat Dec 21, 2013 4:37 pm   

Joined: Mon Jul 01, 2013 7:24 pm
Posts: 19
I have many .zip files in a single dir from which I need to extract a .jpg and rename the extracted .jpg with the same name as the .zip from which it was extracted. For some reason tho I can't seem to get the bash cut command to display anything, nevermind the 14th field which I have approximated to be the name of the .jpg file I want to isolate from the rest of the info unzip -l pukes back.

I am sure I am missing something really simple if someone doesn't mind chiming in to let me know what.

Code:
for zf in *.zip ; do   #include all .zip files in loop
   jf=$(unzip -l ${zf} "*.jpg"|cut -f 14)   #get name of .jpg of current $zf
   unzip ${zf} ${jf}   #extract .jpg $jf from current .zip $zf
   mv ${jf} ${zf}.jpg   #rename the .jpg $jf with same name as .zip $zf
   rename 's/\.zip\.jpg$/\.jpg/' *.zip.jpg   #remove .zip ext from renamed .jpg file
done


Top
 Profile  
 PostPosted: Sun Dec 22, 2013 1:35 pm   

Joined: Mon Jul 01, 2013 7:24 pm
Posts: 19
ok so I finally figured out my issue with the cut comman is that the output from unizp -l command was actually 3 lines of data. I have since switched to using a series of sed commands to better format the output as desired.

here is my modified script sofar that is dealing only with png files at this point:

Code:
for zf in *.zip ; do
   pf=$(unzip -l ${zf} "*.png"|sed -r '/\.png/!d'|sed -r 's/^.*[0-9]{2}:[0-9]{2}[ \t]*[/]{0,1}(.*\.png)/\1/'|sed -r 's/[ ]{1,}/_/g'|sed -r 's/^.*\/(.*)$/\1/')
   echo "unzip ${zf} ${pf}"
   echo "mv ${pf} ${zf}.png"
   echo "rename 's/\.zip\.png$/\.png/' *.zip.png"
done


here is the output from running the above on two test *.zip files. pix-2.png file in one of the *.zip files is contained in a folder in the *.zip file.

Code:
unzip zip-1.zip pix-1.png
mv pix-1.png zip-1.zip.png
rename 's/\.zip\.png$/\.png/' *.zip.png
unzip zip-2.zip pix-2.png
mv pix-2.png zip-2.zip.png
rename 's/\.zip\.png$/\.png/' *.zip.png


But when I actually execute the commands echoing above, there is no problem extracting and renaming pix-1.png from zip-1.zip, but I am guessing because pix-2.png is contained in a folder in zip-2.zip I get the following errors even though pix-2.png can be queried/listed fine:

Code:
Archive:  zip-1.zip
  inflating: pix-1.png     
Archive:  zip-2.zip
caution: filename not matched:  pix-2.png
mv: cannot stat `pix-2.png': No such file or directory
Can't rename *.zip.png *.png: No such file or directory


If someone might kindly be able to enlighten me why a file that can be listed cannot then be infalted I would sure appreciate it.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 30 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP