Register
It is currently Wed Jul 23, 2014 1:51 am

unrar, pack, and move files automatically.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Sun Dec 06, 2009 10:54 am   

Joined: Sun Dec 06, 2009 10:17 am
Posts: 2
Hello everyone.

Being a complete noob in this field, I though I'd ask here.

currently on my server, I have a directory containing several subdirectories. Each one of these subdirectories contains RAR files.

I'm looking for an automated way, if there is any, to UNRAR the files inside each folder, REPACK them to 200MB files, and then move those 200Mb files inside the root directory.
So the process requires unraring, and possible waiting for it to finish, then raring again the extracted files, and then moving those files.

I don't know if this is possible, because of the intervals needed between each process.

If it's too difficult or impossible, I'd be also happy with a script which just PACKS each directory into 200mb files.

any help will be appreciated!

Thanks :)


Top
 Profile  
 PostPosted: Mon Dec 07, 2009 2:38 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Maybe something like this... it's not tested at all so i don't promise anything about how it should work :P
Code:
#!/bin/bash
# Translate argument 1 to usable name
inpath=$1
# Save where you are right now.
oldpwd=$(pwd)
# Check if the directory in argument 1 exists.
if [ -d $inpath ]; then
   cd $inpath;
   mkdir -p original;
   # Move the original files away, this will generate a warning since the "original" directory will be "moved" in this also.
   mv * $inpath/original &> /dev/null;
   mkdir -p $inpath/repacked;
   # Figure out which the first rar archive is, part01 or r00 versions doesn't matter that much.
   relname=$(ls $inpath/original/*.rar|head -n1);
   # Unpack it.
   unrar e -inul $relname $inpath/repacked;
   # If it fails, then exit
   if [ $? -eq 0 ]; then
      # Remove all the crap that we don't need and recreate it using our own standard.
      relname=$(basename $relname | sed -e "s/\(?:\.part01|.rar$\)//g");
      # Our own standard (this is very possibly the same as one of the above, but since there's more then 1 option it's better to recreate it)
      outfile="$inpath/repacked/$relname.rar";
      cd $inpath/repacked;
      # Pack the files into 200Mb files (-v200000k is for 200Mb noted in Kb)
      rar a -m0 -r -v200000k $outfile *;
      # If it succeds we move the files around to the right place
      if [ $? -eq 0]; then
         outpath="$inpath/repacked";
         mv $outpath/* $inpath;
         # Delete the temporary "repacked" directory
         rm -rf $outpath;
      else
         echo "Failed to compress files... exiting...";
      fi
   else
      echo "Failed to extract files ($relname).. exiting";
   fi
fi
# Return to where you started when executing the script.
cd $oldpwd


It takes 1 argument as input, and that's the catalog that you're repacking.
Usage: ./script.sh <path-to-catalog-which-contains-rar-files>

ps. This will keep the original release in a directory named "original", you'll have to delete that manually ds.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Dec 07, 2009 9:44 am   

Joined: Sun Dec 06, 2009 10:17 am
Posts: 2
pretty awesome :) it worked, BUT-

In the end I find myself wiith two directories - original + repack, with the original files still there.

and besides, I get this error message:

Code:
./rar.sh: line 27: [: missing `]'
Failed to compress files... exiting...


Actually, I don't want it to change anything in the original files in the subdirectory... I just want to have the same files, but extracted and repacked in 200MB in the root directory, so the whole moving part isn't needed.

I'm sure it's a piece of cake for you to make these changes :)

Thanks again!!!


Top
 Profile  
 PostPosted: Wed Dec 09, 2009 3:09 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Code:
#!/bin/bash
# Translate argument 1 to usable name
inpath=$1
# Save where you are right now.
oldpwd=$(pwd)
# Check if the directory in argument 1 exists.
if [ -d $inpath ]; then
   cd $inpath;
   mkdir -p original;
   # Move the original files away, this will generate a warning since the "original" directory will be "moved" in this also.
   mv * $inpath/original &> /dev/null;
   mkdir -p $inpath/repacked;
   # Figure out which the first rar archive is, part01 or r00 versions doesn't matter that much.
   relname=$(ls $inpath/original/*.rar|head -n1);
   # Unpack it.
   unrar e -inul $relname $inpath/repacked;
   # If it fails, then exit
   if [ $? -eq 0 ]; then
      # Remove all the crap that we don't need and recreate it using our own standard.
      relname=$(basename $relname | sed -e "s/\(?:\.part01|.rar$\)//g");
      # Our own standard (this is very possibly the same as one of the above, but since there's more then 1 option it's better to recreate it)
      outfile="$inpath/repacked/$relname.rar";
      cd $inpath/repacked;
      # Pack the files into 200Mb files (-v200000k is for 200Mb noted in Kb)
      rar a -m0 -r -v200000k $outfile *;
      # If it succeds we move the files around to the right place
      if [ $? -eq 0 ]; then
         outpath="$inpath/repacked";
         mv $outpath/* $inpath;
         # Delete the temporary "repacked" directory
         rm -rf $outpath;
      else
         echo "Failed to compress files... exiting...";
      fi
   else
      echo "Failed to extract files ($relname).. exiting";
   fi
fi
# Return to where you started when executing the script.
cd $oldpwd


I'm not sure I understand what it is you want.
The original files are never changed, they're just moved to make room for the new files in the repacked catalog... which was supposed to be moved to the original location.

Anyway, the error was that a bracket (]) was against the 0 in "if [ $? -eq 0]"... bash doesn't like that.

What should happen now is the following:
1) Enter the path you put in when starting the script.
2) creates a "temporary" original directory
3) moves the files from the "root" to the original directory
4) creates a temporary directory named "repacked"
5) unrar's the content from the "original" directory to the "repacked" directory
6) if that went okey, then it figures out the name of the rar archive and repacks it in 200Mb files
7) moves the newly created files in the "repacked" directory into the "root" directory (../ that is)

If you want to remove the original files then you can always add a "rm -rf original" at the bottom (above the "cd $oldpwd") and it'll be the last the script does.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Sat Aug 21, 2010 4:36 am   

Joined: Sat Aug 21, 2010 3:36 am
Posts: 14
Nice I needed this :)) Thanks


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

All times are UTC - 6 hours


Who is online

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


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