Create folder from .rar or .r00 file with filename excluding the extension (usinBash)
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Create folder from .rar or .r00 file with filename excluding the extension (usinBash)
Hi, I have been reading posts in this forum for a few days now, and they have been very helpful, but today I got stuck and after a few hours trying to figure out how to get this done I got a way of doing it, but it has still has errors and im sure it's not the proper way.
So I would be greatfull if someone could help me out here.
My question is as follows:
Im trying to find all files that are ether .r00 or .part1.rar in the current directory, which I have done. Then I want to create a folder from each of them and take out the extension in the folder name which it does too but obviously not the correct way, as it still comes up with errors (such as "file exists") but since I do it twice it gets my folders created, but is there a better/correct way of doing this?
Is there a way that I can do something like this: DIRNAME=${i%.r0*0 OR i%.part1.rar} .....
This is what I am doing at the moment:
Code:
#!/bin/bash
for i in `ls *.r0*0 *.part1.rar`; do
DIRNAME=${i%.r0*0}
mkdir $DIRNAME
DIRNAME=${i%.part1.rar}
mkdir $DIRNAME
done
#!/bin/bash
for i in `ls *.r0*0 *.part1.rar`
do
DIRNAME=${i%.r0*0}
if [ "$DIRNAME" == "$i" ]
then
DIRNAME=${i%.part1.rar}
fi
if [ ! -f $DIRNAME ]
then
mkdir $DIRNAME
fi
mv $i $DIRNAME/.
done
Fantastic, thanks Disillusionist! it worked perfectly.
I found out this morning that I needed a 3rd find/replace extension and I managed to cobble together some code from yours and also added a unrar function so it dumps the rar contents into the new folder of the rar files.....
Here is the code im using now (i'v given it here incase some one else one day might need it....)
Code:
#!/bin/bash
for i in `ls *.r0*0 *.part1.rar *.part0*1.rar`
do
DIRNAME=${i%.r0*0}
if [ "$DIRNAME" == "$i" ]
then
DIRNAME=${i%.part1.rar}
fi
if [ "$DIRNAME" == "$i" ]
then
DIRNAME=${i%.part0*1.rar}
fi
if [ ! -f $DIRNAME ]
then
mkdir $DIRNAME
cd $DIRNAME
rar x ../$i
cd ..
fi
# mv $i $DIRNAME/.
done
Last edited by Techno Guy; 12-28-2008 at 02:30 PM.
EDIT: my original code was flawed and would attempt to create a directory each time.
So that you only create the directory once, use:
Code:
if [ ! -f $DIRNAME ]
then
###
### $DIRNAME is not a "regular file"
###
if [ ! -d $DIRNAME ]
then
###
### $DIRNAME is not a directory either (so create it)
###
mkdir $DIRNAME
fi
cd $DIRNAME
rar x ../$i
cd ..
else
echo "$DIRNAME is a file! not processing $i"
fi
Last edited by Disillusionist; 12-29-2008 at 12:10 AM.
Reason: Fixing error in the code
Thanks jschiwal, now that I look at it, yea I can do with out the "ls", i'v taken it out of my other code and it all seems to still be working
Thanks again to Disillusionist, the new code looks much more efficient then the old one, it seems to be working fine too, but I wont know for sure till tomorrow when I get to do a proper test, will report back if there are any issues.
Thanks again to Disillusionist, the new code looks much more efficient then the old one, it seems to be working fine too, but I wont know for sure till tomorrow when I get to do a proper test, will report back if there are any issues.
That was just a snippet, the complete code (with Jschiwal's ammendment) is:
Code:
#!/bin/bash
for FILE in *.r0*0 *.part1.rar *.part0*1.rar
do
DIRNAME=${FILE%.r0*0}
DIRNAME=${DIRNAME%.part1.rar}
DIRNAME=${DIRNAME%.part0*1.rar}
if [ ! -f $DIRNAME ]
then
###
### $DIRNAME is not a "regular file"
###
if [ ! -d $DIRNAME ]
then
###
### $DIRNAME is not a directory either (so create it)
###
mkdir $DIRNAME
fi
cd $DIRNAME
rar x ../$FILE
cd ..
else
echo "$DIRNAME is a file! not processing $FILE"
fi
done
Oh, ok. The incomplete one still kinda worked, until this morning when I tried it with a bunch of files, was wondering if I was missing something but since it worked as was I didn't think any further
But yea im using the new code in my script now and all seems fine now too
Thanks again so much for all your time and help! (maybe one day I will help you )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.