Register
It is currently Fri Oct 24, 2014 10:12 pm

create directory from rar file, unrar file, cp directory


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Sat Oct 10, 2009 12:26 am   

Joined: Fri Dec 05, 2008 12:23 am
Posts: 3
i have a bunch of .rar files from those great music blogs. unrar on freebsd, afaik (from help and man), does not create a directory but merely spews all the files out wherever you desire.

what i want to be able to do is:

1. create two directories - one in my mp3 player and the other on my hard drive - base on the *.rar name, i.e. if i have /hd/foo.rar i want to mkdir /mp3/foo and /hd/foo . typically the *.rar names include only spaces as for special characters.
2. unrar e /hd/foo.rar /hd/foo/ (note if the file demands a password, i imagine the script would break or halt. it would be helpful to know which file causes it.)
3. cp -R /hd/foo/*mp3 /mp3/foo/
4. repeat 1-3 for the rest of the *.rar files

also this isn't necessary to the script, but i am wondering if i missed an option in the man for cp, as it is quite an annoyance to have to make the destination directory by mkdir foo rather than just having a cp option to use the same dir name and make one in the destination.


Top
 Profile  
 PostPosted: Mon Oct 12, 2009 4:28 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
sorry I don't have time for a proper reply, but the command 'rsync' should be able to do the copying functions you want, check the man page :)


Top
 Profile  
 PostPosted: Wed Oct 14, 2009 8:37 pm   

Joined: Sun Oct 11, 2009 10:48 pm
Posts: 1
This is a quickie and I'm not 100% certain this will work, as I don't have any rar files to work with. I did create a text file named "foo off.rar" because it had a space in it, and everything seemed to work and you should be able to edit it to your needs.
Code:
#! /bin/sh

#"foo oof.rar" is my example file

# let's record our current location so we can come back here
origin=$(pwd)

# for everything named *.rar
for line in *.rar

# everything from "do" to "done" will be performed on every rar file
do

# basename will take a path and give you the file name.
# if you know the file extension, add it to the end of the command
# and basename will chop it off in it's output

# this is running the command: basename "foo oof.rar" .rar
# the output should be "foo oof"
name=$(basename "$line" .rar)

# I'm not sure you want to do it this way,
# you may want to edit the next 5 lines
# but it should give you some ideas on ways to do it

# create directory "foo oof"
mkdir "$name"

# move "foo oof.rar" into it
mv "$line" "$name"

# cd into it
cd "$name"

# unrar "foo oof.rar"
"unrar e $line"

# make directory /mp3/"foo oof"/ and copy all mp3s to it
mkdir /mp3/"$name"/
cp *.mp3  /mp3/"$name"/

# go back to the original location so we're in the
#right place to run all this again on the next rar
cd "$origin"

done


Top
 Profile  
 PostPosted: Sat Aug 21, 2010 3:40 am   

Joined: Sat Aug 21, 2010 3:36 am
Posts: 14
fubaya wrote:
This is a quickie and I'm not 100% certain this will work, as I don't have any rar files to work with. I did create a text file named "foo off.rar" because it had a space in it, and everything seemed to work and you should be able to edit it to your needs.
Code:
#! /bin/sh

#"foo oof.rar" is my example file

# let's record our current location so we can come back here
origin=$(pwd)

# for everything named *.rar
for line in *.rar

# everything from "do" to "done" will be performed on every rar file
do

# basename will take a path and give you the file name.
# if you know the file extension, add it to the end of the command
# and basename will chop it off in it's output

# this is running the command: basename "foo oof.rar" .rar
# the output should be "foo oof"
name=$(basename "$line" .rar)

# I'm not sure you want to do it this way,
# you may want to edit the next 5 lines
# but it should give you some ideas on ways to do it

# create directory "foo oof"
mkdir "$name"

# move "foo oof.rar" into it
mv "$line" "$name"

# cd into it
cd "$name"

# unrar "foo oof.rar"
"unrar e $line"

# make directory /mp3/"foo oof"/ and copy all mp3s to it
mkdir /mp3/"$name"/
cp *.mp3  /mp3/"$name"/

# go back to the original location so we're in the
#right place to run all this again on the next rar
cd "$origin"

done


Hi this script looks promising but I doubt it will work with split archives such as archive.part01.rar, part02 etc. or the old naming convention archive.r01 etc.
Is there a way that you can assist with this? "To make it read or find that .r01 or part01.rar.

Many Thanks... :))


Top
 Profile  
 PostPosted: Sat Aug 21, 2010 5:28 am   

Joined: Sat Aug 21, 2010 3:36 am
Posts: 14
Question:

I already asked this somewhere but I think we already have a foundation here so I thought of a follow up question.

What if I just want to rar each ISO files inside a folder?

Code:

# 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


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: 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:  
cron


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