Register
It is currently Mon Sep 01, 2014 1:41 am

Unrar a rar inside a rar????


All times are UTC - 6 hours


Post new topic Reply to topic  [ 10 posts ] 
Author Message
 PostPosted: Fri Aug 27, 2010 7:54 pm   

Joined: Sat Aug 21, 2010 3:36 am
Posts: 14
@-)

Confusing title isn't it?

Well what I have is a directory which contains hundreds of split archive rar files
inside the split archive files is another rar file.

I am doing:
Code:
rar e file.part1.rar  -ppassword
manually and another rar e for the main rar is there a way to automate this as it's a nightmare extracting thousands of files manually. =;


Top
 Profile  
 PostPosted: Sat Aug 28, 2010 12:50 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
I suggest doing it in some kind of loop like this.
It will run a second loop over all 'OK' extracted rar files from the first loop.

Code:
#!/bin/bash

firstfile="firstfile.rar"

for extractedfile in $(unrar e "$firstfile" | awk '/Extracting.*\.rar.*OK/ {print $2}'); do
  unrar e "$extractedfile";
done


Top
 Profile  
 PostPosted: Sun Aug 29, 2010 2:35 pm   

Joined: Sat Aug 21, 2010 3:36 am
Posts: 14
Hi again Patsie thanks for the reply.
scrip is working on only with the first rar archive

I think this part of the code
Code:
unrar e "$extractedfile";
is not working as the files becomes file.001 after the first extract


Top
 Profile  
 PostPosted: Sun Aug 29, 2010 10:49 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Then you have to change the 'awk' part as you see fit.
I checked for all extracted '.rar' fles, but apparently you want to extract '.001' files.
Code:
awk '/Extracting.*\.rar.*OK/ {print $2}');

Change it as you see fit.


Top
 Profile  
 PostPosted: Mon Aug 30, 2010 7:29 pm   

Joined: Sat Aug 21, 2010 3:36 am
Posts: 14
sweet it worked again as always Patsie.

Is it possible to add additional task on the script?

Like

1. check the files with the same prefix
2. create a directory base on the prefix of the files
3. move the files with the same prefix on that directory
4. go to the created directory and extract the files.

Example:

Files with similar prefix:

0001.part1.rar
0001.part2.rar
0001.part3.rar
0002.part1.rar
0002.part2.rar
0002.part3.rar

Logic:

Code:
create folder 0001
move files 0001.* to 0001
cd to 0001
extract 0001.part1.rar


Cheers,
r0cks0ul


Top
 Profile  
 PostPosted: Tue Aug 31, 2010 8:06 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
I would suggest looking at what you want to accomplish, and thinking of the commands you need to accomplish the task.

Then come up with the logic to surround it.

For instance,

r0cks0ul wrote:
Code:
create folder 0001
move files 0001.* to 0001
cd to 0001
extract 0001.part1.rar



You know the commands needed for each of those steps... right?
like... mkdir, mv, cd, and the unrar command...

You also know how you want to glob the commands...

r0cks0ul wrote:
Files with similar prefix:

0001.part1.rar
0001.part2.rar
0001.part3.rar
0002.part1.rar
0002.part2.rar
0002.part3.rar


To get the prefix in that case, I would echo the variable holding the name of the file through cut... like...
Code:
prefix=`echo "$filename" | cut -d. -f1`


Now that you have more pieces of your puzzle... think you can put them together?

Give it a shot!


Top
 Profile  
 PostPosted: Wed Sep 01, 2010 2:48 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
jbsnake wrote:
To get the prefix in that case, I would echo the variable holding the name of the file through cut... like...
Code:
prefix=`echo "$filename" | cut -d. -f1`


It's bashier to use
Code:
prefix=${filename%%.*}
B-)


Top
 Profile  
 PostPosted: Wed Sep 01, 2010 3:02 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
meh... tomato tomato...


Top
 Profile  
 PostPosted: Thu Sep 02, 2010 12:46 am   

Joined: Sat Aug 21, 2010 3:36 am
Posts: 14
Hi guys I managed to do the desired task problem is I need to put the code together but I am having issues:

Here's the code:

Code:
ls *.rar |while read LINE
do
  folder=${LINE%.par*.rar}
  mkdir -p $folder
  mv $LINE $folder
  cd $folder
done


How do I put them together? ? ?

Code:

#!/bin/bash

firstfile="firstfile.rar"

for extractedfile in $(rar e -ppassword "$firstfile" | awk '/Extracting.*\.001.*OK/ {print $2}'); do
  rar e "$extractedfile";
done



Top
 Profile  
 PostPosted: Thu Sep 02, 2010 8:37 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
Depends really on what you are attempting... I would suggest you run it from start to finish in your mind... Ignore the loop mentally...

Take the first file... think about what you want done to it and follow it along step by step.

I do know one thing... the below will cause issue:
r0cks0ul wrote:
Code:
    ls *.rar |while read LINE
    do
      folder=${LINE%.par*.rar}
      mkdir -p $folder
      mv $LINE $folder
      cd $folder
    done



You don't want to cd to the folder (from here on out I will call it a directory, since that's *nix talk) while in the loop, unless you cd back out before the next iteration. Since it seems this portion is just a sorting... I would say you don't need to cd to the directory at all, but you might want to add the directory to a list to cd into later. In fact, you could avoid the system doing the same things over and over by separating the task a little more... like:

Code:
    ls *.rar | cut -d. -f1 | sort -u | while read LINE
    do
      new_dir=${LINE}
      echo ${new_dir} >> new_directory.lst
      mkdir -p ${new_dir}
      mv ${LINE}.* ${new_dir}/
    done


The above gets all the prefixes at once, then goes down the list of those prefixes, adding their name to a file called "new_directory.lst" then making the folder and moving all files with that prefix to that new directory. The list was only made if you needed to go back later and go into each of those folders to do something else (like extract further). You would just have to iterate through the list, cd'ing to the directory, and executing a rar for each file in the directory.

You could also just do the further extraction while in the loop, you just have to make sure of your path while you work... like:
Code:
    ls *.rar | cut -d. -f1 | sort -u | while read LINE
    do
      new_dir=${LINE}
    #  echo ${new_dir} >> new_directory.lst # don't really need this, but you can still use it to keep track for clean up...
      mkdir -p ${new_dir}
      mv ${LINE}.* ${new_dir}/
      cd ${new_dir}
     # do some extracting further...
     # or not...
     cd - # go back to the previous directory
    done


As far as putting them together, it depends on what comes first. Again, just step through your goal from the start to the finish.

HTH


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 7 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