LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 12-28-2008, 02:42 AM   #1
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Rep: Reputation: 15
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
Your help would be greatly appreciated.
 
Old 12-28-2008, 05:21 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Welcome to LQ.

Try the following:

Code:
#!/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
 
Old 12-28-2008, 02:28 PM   #3
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Original Poster
Rep: Reputation: 15
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.
 
Old 12-28-2008, 03:30 PM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Code:
for i in `ls *.r0*0 *.part1.rar *.part0*1.rar`
The ls command isn't needed. Why do people use it? The expansion occurs before the ls command is run.

This will suffice:
Code:
for i in *.r0*0 *.part1.rar *.part0*1.rar

Last edited by jschiwal; 12-28-2008 at 04:41 PM.
 
Old 12-28-2008, 04:05 PM   #5
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
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
 
Old 01-03-2009, 02:09 AM   #6
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Original Poster
Rep: Reputation: 15
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.
 
Old 01-03-2009, 04:35 AM   #7
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Quote:
Originally Posted by Techno Guy View Post
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
 
Old 01-04-2009, 12:13 AM   #8
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Original Poster
Rep: Reputation: 15
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 )
 
  


Reply

Tags
rar


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
create a timestamped rar file steve51184 Linux - Software 2 05-22-2008 04:36 AM
ERROR :: The file or folder smb://<I,P.>/<Filename> does not exist rushadrenaline Linux - Networking 12 02-02-2008 12:18 AM
create a folder/file within a folder using perl indu.a Programming 2 03-07-2007 10:09 PM
how can i default the max folder file size when it create inside a folder antony_csf Linux - Software 1 06-17-2004 02:26 AM
I have 8 character file name, no extension files being created in my dbaseIII+ folder bonzo Linux - General 2 01-22-2004 06:49 AM


All times are GMT -5. The time now is 02:23 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration